Peter's keyboard firmware (QMK) https://qmk.fm/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xtonhasvim.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /* Copyright 2015-2017 Christon DeWan
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "xtonhasvim.h"
  17. #include "fancylighting.h"
  18. /************************************
  19. * helper foo
  20. ************************************/
  21. #define PRESS(kc) register_code(kc)
  22. #define RELEASE(kc) unregister_code(kc)
  23. static void TAP(uint16_t keycode) {
  24. PRESS(keycode);
  25. RELEASE(keycode);
  26. }
  27. static void CMD(uint16_t keycode) {
  28. PRESS(KC_LGUI);
  29. TAP(keycode);
  30. RELEASE(KC_LGUI);
  31. }
  32. static void CTRL(uint16_t keycode) {
  33. PRESS(KC_LCTRL);
  34. TAP(keycode);
  35. RELEASE(KC_LCTRL);
  36. }
  37. static void SHIFT(uint16_t keycode) {
  38. PRESS(KC_LSHIFT);
  39. TAP(keycode);
  40. RELEASE(KC_LSHIFT);
  41. }
  42. static void ALT(uint16_t keycode) {
  43. PRESS(KC_LALT);
  44. TAP(keycode);
  45. RELEASE(KC_LALT);
  46. }
  47. uint16_t vstate = VIM_START;
  48. static bool yank_was_lines = false;
  49. static bool SHIFTED = false;
  50. static uint32_t mod_override_layer_state = 0;
  51. static uint16_t mod_override_triggering_key = 0;
  52. static void edit(void) { vstate = VIM_START; layer_clear(); }
  53. #define EDIT edit()
  54. static void simple_movement(uint16_t keycode) {
  55. switch(keycode) {
  56. case VIM_B:
  57. PRESS(KC_LALT);
  58. SHIFT(KC_LEFT); // select to start of this word
  59. RELEASE(KC_LALT);
  60. break;
  61. case VIM_E:
  62. PRESS(KC_LALT);
  63. SHIFT(KC_RIGHT); // select to end of this word
  64. RELEASE(KC_LALT);
  65. break;
  66. case VIM_H:
  67. SHIFT(KC_LEFT);
  68. break;
  69. case VIM_J:
  70. CMD(KC_LEFT);
  71. SHIFT(KC_DOWN);
  72. SHIFT(KC_DOWN);
  73. break;
  74. case VIM_K:
  75. CMD(KC_LEFT);
  76. TAP(KC_DOWN);
  77. SHIFT(KC_UP);
  78. SHIFT(KC_UP);
  79. break;
  80. case VIM_L:
  81. SHIFT(KC_RIGHT);
  82. break;
  83. case VIM_W:
  84. PRESS(KC_LALT);
  85. SHIFT(KC_RIGHT); // select to end of this word
  86. SHIFT(KC_RIGHT); // select to end of next word
  87. SHIFT(KC_LEFT); // select to start of next word
  88. RELEASE(KC_LALT);
  89. break;
  90. }
  91. }
  92. static void comma_period(uint16_t keycode) {
  93. switch (keycode) {
  94. case VIM_COMMA:
  95. if (SHIFTED) {
  96. // indent
  97. CMD(KC_LBRACKET);
  98. } else {
  99. // toggle comment
  100. CMD(KC_SLASH);
  101. }
  102. break;
  103. case VIM_PERIOD:
  104. if (SHIFTED) {
  105. // outdent
  106. CMD(KC_RBRACKET);
  107. }
  108. break;
  109. }
  110. }
  111. __attribute__ ((weak))
  112. bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  113. return true;
  114. }
  115. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  116. /* keymap gets first whack */
  117. if(!process_record_keymap(keycode, record)) return false;
  118. /****** FIREY_RETURN *****/
  119. if(record->event.pressed && keycode == FIREY_RETURN) {
  120. start_firey_return();
  121. TAP(KC_ENT);
  122. }
  123. /****** mod passthru *****/
  124. if(record->event.pressed && layer_state_is(vim_cmd_layer()) && (IS_MOD(keycode) || keycode == LSFT(KC_LALT))) {
  125. mod_override_layer_state = layer_state;
  126. mod_override_triggering_key = keycode;
  127. // TODO: change this to track key location instead
  128. layer_clear();
  129. return true; // let the event fall through...
  130. }
  131. if(mod_override_layer_state && !record->event.pressed && keycode == mod_override_triggering_key) {
  132. layer_state_set(mod_override_layer_state);
  133. mod_override_layer_state = 0;
  134. mod_override_triggering_key = 0;
  135. return true;
  136. }
  137. if (VIM_START <= keycode && keycode <= VIM_ESC) {
  138. if(keycode == VIM_SHIFT) {
  139. SHIFTED = record->event.pressed;
  140. return false;
  141. }
  142. if (record->event.pressed) {
  143. if(keycode == VIM_START) {
  144. // entry from anywhere
  145. layer_on(vim_cmd_layer());
  146. vstate = VIM_START;
  147. // reset state
  148. yank_was_lines = false;
  149. SHIFTED = false;
  150. mod_override_layer_state = 0;
  151. mod_override_triggering_key = 0;
  152. return false;
  153. }
  154. switch(vstate) {
  155. case VIM_START:
  156. switch(keycode){
  157. /*****************************
  158. * ground state
  159. *****************************/
  160. case VIM_A:
  161. if(SHIFTED) {
  162. // CMD(KC_RIGHT);
  163. CTRL(KC_E);
  164. } else {
  165. TAP(KC_RIGHT);
  166. }
  167. EDIT;
  168. break;
  169. case VIM_B:
  170. PRESS(KC_LALT);
  171. PRESS(KC_LEFT);
  172. break;
  173. case VIM_C:
  174. if(SHIFTED) {
  175. PRESS(KC_LSHIFT);
  176. CMD(KC_RIGHT);
  177. RELEASE(KC_LSHIFT);
  178. CMD(KC_X);
  179. yank_was_lines = false;
  180. EDIT;
  181. } else {
  182. vstate = VIM_C;
  183. }
  184. break;
  185. case VIM_D:
  186. if(SHIFTED) {
  187. CTRL(KC_K);
  188. } else {
  189. vstate = VIM_D;
  190. }
  191. break;
  192. case VIM_E:
  193. PRESS(KC_LALT);
  194. PRESS(KC_RIGHT);
  195. break;
  196. case VIM_G:
  197. if(SHIFTED) {
  198. TAP(KC_END);
  199. } else {
  200. vstate = VIM_G;
  201. }
  202. break;
  203. case VIM_H:
  204. PRESS(KC_LEFT);
  205. break;
  206. case VIM_I:
  207. if(SHIFTED){
  208. CTRL(KC_A);
  209. }
  210. EDIT;
  211. break;
  212. case VIM_J:
  213. if(SHIFTED) {
  214. CMD(KC_RIGHT);
  215. TAP(KC_DEL);
  216. } else {
  217. PRESS(KC_DOWN);
  218. }
  219. break;
  220. case VIM_K:
  221. PRESS(KC_UP);
  222. break;
  223. case VIM_L:
  224. PRESS(KC_RIGHT);
  225. break;
  226. case VIM_O:
  227. if(SHIFTED) {
  228. CMD(KC_LEFT);
  229. TAP(KC_ENTER);
  230. TAP(KC_UP);
  231. EDIT;
  232. } else {
  233. CMD(KC_RIGHT);
  234. TAP(KC_ENTER);
  235. EDIT;
  236. }
  237. break;
  238. case VIM_P:
  239. if(SHIFTED) {
  240. CMD(KC_LEFT);
  241. CMD(KC_V);
  242. } else {
  243. if(yank_was_lines) {
  244. CMD(KC_RIGHT);
  245. TAP(KC_RIGHT);
  246. CMD(KC_V);
  247. } else {
  248. CMD(KC_V);
  249. }
  250. }
  251. break;
  252. case VIM_S:
  253. // s for substitute?
  254. if(SHIFTED) {
  255. CMD(KC_LEFT);
  256. PRESS(KC_LSHIFT);
  257. CMD(KC_RIGHT);
  258. RELEASE(KC_LSHIFT);
  259. CMD(KC_X);
  260. yank_was_lines = false;
  261. EDIT;
  262. } else {
  263. SHIFT(KC_RIGHT);
  264. CMD(KC_X);
  265. yank_was_lines = false;
  266. EDIT;
  267. }
  268. break;
  269. case VIM_U:
  270. if(SHIFTED) {
  271. PRESS(KC_LSFT);
  272. CMD(KC_Z);
  273. RELEASE(KC_LSHIFT);
  274. } else {
  275. CMD(KC_Z);
  276. }
  277. break;
  278. case VIM_V:
  279. if(SHIFTED) {
  280. CMD(KC_LEFT);
  281. SHIFT(KC_DOWN);
  282. vstate = VIM_VS;
  283. } else {
  284. vstate = VIM_V;
  285. }
  286. break;
  287. case VIM_W:
  288. PRESS(KC_LALT);
  289. TAP(KC_RIGHT);
  290. TAP(KC_RIGHT);
  291. TAP(KC_LEFT);
  292. RELEASE(KC_LALT);
  293. break;
  294. case VIM_X:
  295. // SHIFT(KC_RIGHT);
  296. // CMD(KC_X);
  297. PRESS(KC_DEL);
  298. break;
  299. case VIM_Y:
  300. if(SHIFTED) {
  301. CMD(KC_LEFT);
  302. SHIFT(KC_DOWN);
  303. CMD(KC_C);
  304. TAP(KC_RIGHT);
  305. yank_was_lines = true;
  306. } else {
  307. vstate = VIM_Y;
  308. }
  309. break;
  310. case VIM_COMMA:
  311. case VIM_PERIOD:
  312. comma_period(keycode);
  313. break;
  314. }
  315. break;
  316. case VIM_C:
  317. /*****************************
  318. * c- ...for change. I never use this...
  319. *****************************/
  320. switch(keycode) {
  321. case VIM_B:
  322. case VIM_E:
  323. case VIM_H:
  324. case VIM_J:
  325. case VIM_K:
  326. case VIM_L:
  327. case VIM_W:
  328. simple_movement(keycode);
  329. CMD(KC_X);
  330. yank_was_lines = false;
  331. EDIT;
  332. break;
  333. case VIM_C:
  334. CMD(KC_LEFT);
  335. PRESS(KC_LSHIFT);
  336. CMD(KC_RIGHT);
  337. RELEASE(KC_LSHIFT);
  338. CMD(KC_X);
  339. yank_was_lines = false;
  340. EDIT;
  341. break;
  342. case VIM_I:
  343. vstate = VIM_CI;
  344. break;
  345. default:
  346. vstate = VIM_START;
  347. break;
  348. }
  349. break;
  350. case VIM_CI:
  351. /*****************************
  352. * ci- ...change inner word
  353. *****************************/
  354. switch(keycode) {
  355. case VIM_W:
  356. ALT(KC_LEFT);
  357. PRESS(KC_LSHIFT);
  358. ALT(KC_RIGHT);
  359. RELEASE(KC_LSHIFT);
  360. CMD(KC_X);
  361. yank_was_lines = false;
  362. EDIT;
  363. default:
  364. vstate = VIM_START;
  365. break;
  366. }
  367. break;
  368. case VIM_D:
  369. /*****************************
  370. * d- ...delete stuff
  371. *****************************/
  372. switch(keycode) {
  373. case VIM_B:
  374. case VIM_E:
  375. case VIM_H:
  376. case VIM_J:
  377. case VIM_K:
  378. case VIM_L:
  379. case VIM_W:
  380. simple_movement(keycode);
  381. CMD(KC_X);
  382. yank_was_lines = false;
  383. vstate = VIM_START;
  384. break;
  385. case VIM_D:
  386. CMD(KC_LEFT);
  387. SHIFT(KC_DOWN);
  388. CMD(KC_X);
  389. yank_was_lines = true;
  390. vstate = VIM_START;
  391. break;
  392. case VIM_I:
  393. vstate = VIM_DI;
  394. break;
  395. default:
  396. vstate = VIM_START;
  397. break;
  398. }
  399. break;
  400. case VIM_DI:
  401. /*****************************
  402. * ci- ...delete a word... FROM THE INSIDE!
  403. *****************************/
  404. switch(keycode) {
  405. case VIM_W:
  406. ALT(KC_LEFT);
  407. PRESS(KC_LSHIFT);
  408. ALT(KC_RIGHT);
  409. RELEASE(KC_LSHIFT);
  410. CMD(KC_X);
  411. yank_was_lines = false;
  412. vstate = VIM_START;
  413. default:
  414. vstate = VIM_START;
  415. break;
  416. }
  417. break;
  418. case VIM_V:
  419. /*****************************
  420. * visual!
  421. *****************************/
  422. switch(keycode) {
  423. case VIM_D:
  424. case VIM_X:
  425. CMD(KC_X);
  426. yank_was_lines = false;
  427. vstate = VIM_START;
  428. break;
  429. case VIM_B:
  430. PRESS(KC_LALT);
  431. PRESS(KC_LSHIFT);
  432. PRESS(KC_LEFT);
  433. // leave open for key repeat
  434. break;
  435. case VIM_E:
  436. PRESS(KC_LALT);
  437. PRESS(KC_LSHIFT);
  438. PRESS(KC_RIGHT);
  439. // leave open for key repeat
  440. break;
  441. case VIM_H:
  442. PRESS(KC_LSHIFT);
  443. PRESS(KC_LEFT);
  444. break;
  445. case VIM_I:
  446. vstate = VIM_VI;
  447. break;
  448. case VIM_J:
  449. PRESS(KC_LSHIFT);
  450. PRESS(KC_DOWN);
  451. break;
  452. case VIM_K:
  453. PRESS(KC_LSHIFT);
  454. PRESS(KC_UP);
  455. break;
  456. case VIM_L:
  457. PRESS(KC_LSHIFT);
  458. PRESS(KC_RIGHT);
  459. break;
  460. case VIM_W:
  461. PRESS(KC_LALT);
  462. SHIFT(KC_RIGHT); // select to end of this word
  463. SHIFT(KC_RIGHT); // select to end of next word
  464. SHIFT(KC_LEFT); // select to start of next word
  465. RELEASE(KC_LALT);
  466. break;
  467. case VIM_P:
  468. CMD(KC_V);
  469. vstate = VIM_START;
  470. break;
  471. case VIM_Y:
  472. CMD(KC_C);
  473. TAP(KC_RIGHT);
  474. yank_was_lines = false;
  475. vstate = VIM_START;
  476. break;
  477. case VIM_V:
  478. case VIM_ESC:
  479. TAP(KC_RIGHT);
  480. vstate = VIM_START;
  481. break;
  482. case VIM_COMMA:
  483. case VIM_PERIOD:
  484. comma_period(keycode);
  485. break;
  486. default:
  487. // do nothing
  488. break;
  489. }
  490. break;
  491. case VIM_VI:
  492. /*****************************
  493. * vi- ...select a word... FROM THE INSIDE!
  494. *****************************/
  495. switch(keycode) {
  496. case VIM_W:
  497. ALT(KC_LEFT);
  498. PRESS(KC_LSHIFT);
  499. ALT(KC_RIGHT);
  500. RELEASE(KC_LSHIFT);
  501. vstate = VIM_V;
  502. default:
  503. // ignore
  504. vstate = VIM_V;
  505. break;
  506. }
  507. break;
  508. case VIM_VS:
  509. /*****************************
  510. * visual line
  511. *****************************/
  512. switch(keycode) {
  513. case VIM_D:
  514. case VIM_X:
  515. CMD(KC_X);
  516. yank_was_lines = true;
  517. vstate = VIM_START;
  518. break;
  519. case VIM_J:
  520. PRESS(KC_LSHIFT);
  521. PRESS(KC_DOWN);
  522. break;
  523. case VIM_K:
  524. PRESS(KC_LSHIFT);
  525. PRESS(KC_UP);
  526. break;
  527. case VIM_Y:
  528. CMD(KC_C);
  529. yank_was_lines = true;
  530. TAP(KC_RIGHT);
  531. vstate = VIM_START;
  532. break;
  533. case VIM_P:
  534. CMD(KC_V);
  535. vstate = VIM_START;
  536. break;
  537. case VIM_V:
  538. case VIM_ESC:
  539. TAP(KC_RIGHT);
  540. vstate = VIM_START;
  541. break;
  542. case VIM_COMMA:
  543. case VIM_PERIOD:
  544. comma_period(keycode);
  545. break;
  546. default:
  547. // do nothing
  548. break;
  549. }
  550. break;
  551. case VIM_G:
  552. /*****************************
  553. * gg, and a grab-bag of other macros i find useful
  554. *****************************/
  555. switch(keycode) {
  556. case VIM_G:
  557. TAP(KC_HOME);
  558. break;
  559. // codes b
  560. case VIM_H:
  561. CTRL(KC_A);
  562. break;
  563. case VIM_J:
  564. PRESS(KC_PGDN);
  565. break;
  566. case VIM_K:
  567. PRESS(KC_PGUP);
  568. break;
  569. case VIM_L:
  570. CTRL(KC_E);
  571. break;
  572. default:
  573. // do nothing
  574. break;
  575. }
  576. vstate = VIM_START;
  577. break;
  578. case VIM_Y:
  579. /*****************************
  580. * yoink!
  581. *****************************/
  582. switch(keycode) {
  583. case VIM_B:
  584. case VIM_E:
  585. case VIM_H:
  586. case VIM_J:
  587. case VIM_K:
  588. case VIM_L:
  589. case VIM_W:
  590. simple_movement(keycode);
  591. CMD(KC_C);
  592. TAP(KC_RIGHT);
  593. yank_was_lines = false;
  594. break;
  595. case VIM_Y:
  596. CMD(KC_LEFT);
  597. SHIFT(KC_DOWN);
  598. CMD(KC_C);
  599. TAP(KC_RIGHT);
  600. yank_was_lines = true;
  601. break;
  602. default:
  603. // NOTHING
  604. break;
  605. }
  606. vstate = VIM_START;
  607. break;
  608. }
  609. } else {
  610. /************************
  611. * key release events
  612. ************************/
  613. clear_keyboard();
  614. }
  615. return false;
  616. } else {
  617. return true;
  618. }
  619. }