summaryrefslogtreecommitdiff
path: root/users/art/art.c
blob: 5596e237f8e541a2be4a8407d7929b45514200c4 (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
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
#include "art.h"
#include "string.h"

__attribute__ ((weak))
bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  return true;
}

__attribute__ ((weak))
void led_show_current_os(void) {
}

static bool mac_ctrl_on = false; //for switching tabs
static bool mac_gui_on = false; //for switching languages
static bool mac_alt_tab_on = false; //for switching windows

static const char *key_up[2] = {SS_UP(X_LALT), SS_UP(X_LCTL)};
static const char *key_down[2] = {SS_DOWN(X_LALT), SS_DOWN(X_LCTL)};

int char_to_del = 1;
static bool sarcasm_on = false;
static bool sarcasm_key = false;

void backspace_n_times(int times) {
  for (int i=0; i<times; i++) {
    SEND_STRING(SS_TAP(X_BSPC));  
  }
}

void send_string_remembering_length(char *string) {
  send_string(string);
  char_to_del = strlen(string);
}

void send_shifted_strings(char *string1, char *string2) {
  if ( get_mods() & MOD_MASK_SHIFT ) {
    clear_mods();
    send_string_remembering_length(string2);
  } else {
    send_string_remembering_length(string1);
  }
}

void send_shifted_strings_add(char *string1, char *string2) {
  bool shifted = get_mods() & MOD_MASK_SHIFT;
  clear_mods();

  send_string_remembering_length(string1);

  if (shifted) {
    send_string(string2);
    char_to_del = strlen(string1) + strlen(string2);
  }
}

bool is_mac_with_base_layer_off(void) {
  return !is_win && !layer_state_is(BASE);
}

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  if (sarcasm_on) {
    sarcasm_key = ! sarcasm_key;  
    if (sarcasm_key) {
      SEND_STRING(SS_TAP(X_CAPS));
    }
  }

  //Checking all other non-backspace keys to clear the backspace buffer. This is to prevent the bug of deleting N chars sometime after using a macro
  if (record->event.pressed && (keycode != KC_BSPACE && keycode != XXXXXXX)) {
    char_to_del = 1;
  }

  switch (keycode) {
    case KC_TAB:
      if (record->event.pressed && is_mac_with_base_layer_off()) {
        uint8_t mods = get_mods();
        uint8_t mod_state = mods & MOD_MASK_ALT;
        if (get_mods() & mod_state) {
          del_mods(mod_state);
          add_mods(MOD_LCTL);
          mac_alt_tab_on = true;
        }

        mod_state = mods & MOD_MASK_CTRL;
        if (get_mods() & mod_state && !mac_alt_tab_on) {
          del_mods(mod_state);
          add_mods(MOD_LGUI);
          mac_ctrl_on = true;
        }
      }
      break;
    case KC_LSFT:
      if (record->event.pressed && is_mac_with_base_layer_off()) {
        uint8_t mods = get_mods();
        uint8_t mod_state = mods & MOD_MASK_AG;
        if (get_mods() & mod_state) {
          del_mods(mod_state);
          add_mods(MOD_LGUI);
          mac_gui_on = true;
          SEND_STRING(SS_TAP(X_SPACE));
          return false;
        } else {
          return true;
        }
      }
      break;
    case KC_LEFT:
    case KC_RIGHT:
      if (record->event.pressed && is_mac_with_base_layer_off()) {
        /* && !mac_ctrl_on/!mac_alt_tab_on are required since setting the state while holding the key changes
        the modifier from OS's perspective. As a result, just the pressed key cannot be the single source
        of truth to determine which state we're in, and a separate bool is required */
        uint8_t mods = get_mods();
        uint8_t mod_state = mods & MOD_MASK_ALT;
        //Allows Ctrl <-/-> on Mac if Ctrl Tab is already pressed
        if (get_mods() & mod_state && mac_alt_tab_on && !mac_ctrl_on) {
          del_mods(mod_state);
          add_mods(MOD_LCTL);
        }

        mod_state = mods & MOD_MASK_CTRL;
        if (get_mods() & mod_state && !mac_alt_tab_on) {
          del_mods(mod_state);
          add_mods(MOD_LALT);
          mac_ctrl_on = true;
        }
      }
      break;
    case KC_DEL:
      if (record->event.pressed && is_mac_with_base_layer_off()) {
        uint8_t mod_state = get_mods() & MOD_MASK_CTRL;
        if (get_mods() & mod_state) {
          del_mods(mod_state);
          add_mods(MOD_LALT);
          mac_ctrl_on = true;
        }
      }
      break;
    case KC_LALT:
      if (!record->event.pressed && is_mac_with_base_layer_off()) {
        if (mac_alt_tab_on) {
          unregister_mods(MOD_LCTL);
          mac_alt_tab_on = false;
          return false;
        } else if (mac_gui_on) {
          SEND_STRING(SS_UP(X_LGUI));
          mac_gui_on = false;
          return false;
        }
      }
      break;
    case KC_RALT:
      if (!record->event.pressed && mac_alt_tab_on && is_mac_with_base_layer_off()) {
        unregister_mods(MOD_LCTL);
        mac_alt_tab_on = false;
        return false;
      }
      break;
    case KC_LCTL:
    case KC_RCTL:
      if (!record->event.pressed && mac_ctrl_on && is_mac_with_base_layer_off()) {
        SEND_STRING(SS_UP(X_LGUI) SS_UP(X_LALT));
        mac_ctrl_on = false;
        return false;
      }
      break;

    case KC_HOME:
      if (record->event.pressed && is_mac_with_base_layer_off()) {
        SEND_STRING(SS_LCTL(SS_TAP(X_LEFT)));
        return false;
      }
      break;
    case KC_END:
      if (record->event.pressed && is_mac_with_base_layer_off()) {
        SEND_STRING(SS_LCTL(SS_TAP(X_RIGHT)));
        return false;
      }
      break;
    case KC_BSPC:
      if (record->event.pressed) {
        if (char_to_del > 1) {
          layer_off(GIT_C);
          layer_off(GIT_S);
          backspace_n_times(char_to_del);
          char_to_del = 1;
          return false;
        }

        if (is_mac_with_base_layer_off()) {
          uint8_t mod_state = get_mods() & MOD_MASK_CTRL;
          if (get_mods() & mod_state) {
            del_mods(mod_state);
            add_mods(MOD_LALT);
            mac_ctrl_on = true;
          }
        }
      }
      break;

    /* -------------------------------------------------------------------------
     *                            CUSTOM MACROS
     * ------------------------------------------------------------------------ */
    case CTRL_CTV:
      if (record->event.pressed) {
        if ( get_mods() & MOD_MASK_SHIFT ) {
          clear_mods();
          SEND_STRING(SS_LCTL("ctv"));
        } else {
          SEND_STRING(SS_LCTL("ctv") SS_TAP(X_ENTER));
        }
      }
      break;
    case CTRL_LCTV:
      if (record->event.pressed) {
        if ( get_mods() & MOD_MASK_SHIFT ) {
          //Firefox
          clear_mods();
          SEND_STRING(SS_LCTL("lcP"));
          wait_ms(200);
          SEND_STRING(SS_LCTL("v") SS_TAP(X_ENTER));
        } else if ( get_mods() & MOD_MASK_CTRL ) {
          //Chrome
          clear_mods();
          SEND_STRING(SS_LCTL("lcNv") SS_TAP(X_ENTER));
        } else {
          SEND_STRING(SS_LCTL("lctv"));
        }
      }
      break;
    case CTRL_CAV:
      if (record->event.pressed) {
        SEND_STRING(SS_LCTL("c" SS_TAP(X_TAB)));
        wait_ms(50);
        SEND_STRING(SS_LCTL("av"));
      }
      break;
    case SARCASM:
      if (record->event.pressed) {
        sarcasm_on = !sarcasm_on;
      }
      break;

    /* -------------------------------------------------------------------------
     *                            OS TOGGLING
     * ------------------------------------------------------------------------ */
    case TOG_OS:
      if (record->event.pressed) {
        is_win = ! is_win;
        led_show_current_os();
      }
      break;
    case CTR_ALT:
      if (record->event.pressed) {
        send_string(key_down[is_win]);
      } else {
        send_string(key_up[is_win]);
      }
      break;
    case OS_CTRL:
      if (is_win) {
        if (record->event.pressed) {
          SEND_STRING(SS_DOWN(X_LCTL));
        } else {
          SEND_STRING(SS_UP(X_LCTL));
        }
      } else {
        if (record->event.pressed) {
          SEND_STRING(SS_DOWN(X_LGUI));
        } else {
          SEND_STRING(SS_UP(X_LGUI));
        }
      }
      break;
    case OS_WIN:
      if (is_win) {
        if (record->event.pressed) {
          SEND_STRING(SS_DOWN(X_LGUI));
        } else {
          SEND_STRING(SS_UP(X_LGUI));
        }
      } else {
        if (record->event.pressed) {
          SEND_STRING(SS_DOWN(X_LCTL));
        } else {
          SEND_STRING(SS_UP(X_LCTL));
        }
      }
      break;

    /* -------------------------------------------------------------------------
     *                            STRING MACROS
     * ------------------------------------------------------------------------ */
    // case :
    //   if (record->event.pressed) {
    //     send_string_remembering_length("");
    //   }
    //   break;
    // case :
    //   if (record->event.pressed) {
    //     send_string_remembering_length("", "");
    //   }
    //   break;
    case TILD_BLOCK:
      if (record->event.pressed) {
        SEND_STRING("```" SS_LSFT(SS_TAP(X_ENTER) SS_TAP(X_ENTER)) "```" SS_TAP(X_UP));
        char_to_del = 4;
      }
      break;
    case ADMINS:
      if (record->event.pressed) {
        send_shifted_strings_add("admin", "/aurora/status");
      }
      break;
    case PRESCRIPTION:
      if (record->event.pressed) {
        SEND_STRING("55\t12122019\t");
        char_to_del = 8;
      }
      break;
    case FOURS:
      if (record->event.pressed) {
        SEND_STRING("4444333322221111\t1\t12\t21\t123\n");
        char_to_del = 16;
      }
      break;
      
  case G_ADD:
    if (record->event.pressed) {
      send_string_remembering_length("git add ");
    }
    break;
  case G_BRCH:
    if (record->event.pressed) {
      send_shifted_strings_add("git branch ", "-d ");
    }
    break;
  case G_C:
    if (record->event.pressed) {
      send_string_remembering_length("git c[Heckout/Ommit]");
      layer_on(GIT_C);
    }
    break;
  case G_CHEC:
    if (!record->event.pressed) {
      bool shifted = get_mods() & MOD_MASK_SHIFT;
      clear_mods();
            
      backspace_n_times(15);
      SEND_STRING("heckout ");
      char_to_del = 13;
      if (shifted) {
        SEND_STRING("-b ");
        char_to_del = 16;
      }
      layer_off(GIT_C);
    }
    break;
  case G_COMM:
    if (!record->event.pressed) {
      bool shifted = get_mods() & MOD_MASK_SHIFT;
      clear_mods();
      
      backspace_n_times(15);
      SEND_STRING("ommit -");
      char_to_del = 15;
      if (shifted) {
        SEND_STRING("a");
        char_to_del = 16;
      }
      SEND_STRING("m \"\"" SS_TAP(X_LEFT));
      layer_off(GIT_C);
    }
    break;
  case G_DEV:
    if (record->event.pressed) {
      send_shifted_strings("develop", "master");
    }
    break;
  case G_DIFF:
    if (record->event.pressed) {
      send_string_remembering_length("git diff ");
    }
    break;	
  case G_FTCH:
    if (record->event.pressed) {
      send_string_remembering_length("git fetch ");
    }
    break;
  case G_LOG:
    if (record->event.pressed) {
      send_string_remembering_length("git log ");
    }
    break;
  case G_MERG:
    if (record->event.pressed) {
      send_string_remembering_length("git merge ");
    }
    break;
  case G_P:
    if (record->event.pressed) {
      send_shifted_strings_add("git pu", "sh -u ");
    }
    break;
  case G_RST:
    if (record->event.pressed) {
      send_string_remembering_length("git reset ");
    }
    break;
  case G_S:
    if (!record->event.pressed) {
      send_string_remembering_length("git s[taSh/How/taTus]");
      layer_on(GIT_S);			
    }
    break;
  case G_SHOW:
    if (!record->event.pressed) {
      backspace_n_times(16);
      SEND_STRING("how ");
      char_to_del = 9;
      layer_off(GIT_S);
    }
    break;			
  case G_STSH:
    if (!record->event.pressed) {
      bool shifted = get_mods() & MOD_MASK_SHIFT;
      clear_mods();

      backspace_n_times(16);
      SEND_STRING("tash ");
      char_to_del = 10;

      if (shifted) {
        clear_mods();
        SEND_STRING("apply ");

        char_to_del = 16;
      }

      layer_off(GIT_S);
    }
    break;		
  case G_STAT:
    if (!record->event.pressed) {
      backspace_n_times(16);
      SEND_STRING("tatus ");
      char_to_del = 11;
      layer_off(GIT_S);
    }
    break;

  case CTL_ALT_START ... CTL_ALT_END:
    if (record->event.pressed) {
      if (is_win) {
        tap_code16(LCTL(keycode - CTL_ALT_START));
      } else {
        tap_code16(LALT(keycode - CTL_ALT_START));
      }
    }
    break;
  }

  return process_record_keymap(keycode, record);
}