#include QMK_KEYBOARD_H
#include <assert.h>
enum custom_keycodes {
KC_GREEK = SAFE_RANGE,
KC_CADET,
KC_LAYER_LOCK,
KC_ACCENT_START,
KC_CGRV = KC_ACCENT_START, KC_CAGU, KC_CDIA, KC_CCIR, KC_CCED, KC_CTIL, KC_ACCENT_END,
};
enum layers_keymap {
_QWERTY = 0,
_FUNCTION,
_GREEK,
_SHIFTGREEK,
_CADET,
_SHIFTCADET,
};
#define H(x) UC(0x##x)
#define MO_FN MO(_FUNCTION)
#define KC_LLCK KC_LAYER_LOCK
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[_QWERTY] = LAYOUT_hotswap(
KC_GESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_HOME, KC_END, KC_PGUP, KC_PGDN, KC_MPLY, KC_BRK,
KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_LLCK, KC_PSLS, KC_PAST, KC_PMNS,
KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_P7, KC_P8, KC_P9,
KC_LCTL, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, KC_P4, KC_P5, KC_P6, KC_PPLS,
KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, KC_P1, KC_P2, KC_P3,
KC_LCTL, KC_LALT, KC_LGUI, KC_SPC, MO_FN, KC_GREEK,KC_CADET,KC_LEFT, KC_DOWN, KC_RGHT, KC_P0, KC_PDOT, KC_PENT),
[_GREEK] = LAYOUT_hotswap(
KC_GRV, H(00b9), H(00b2), H(00b3), H(2074), H(2075), H(2076), H(2077), H(2078), H(2079), H(2070), H(207b), H(207a), H(207d), H(207e), XXXXXXX, XXXXXXX, XXXXXXX, _______,
KC_GRV, _______, _______, _______, _______, _______, _______, _______, H(00b0), _______, _______, H(221d), H(223c), _______, _______, H(2298), H(2299), H(2296),
_______, H(03b8), H(03c9), H(03b5), H(03c1), H(03c4), H(03c8), H(03c5), H(03b9), H(03bf), H(03c0), KC_LBRC, KC_RBRC, KC_BSLS, KC_P7, KC_P8, KC_P9,
_______, H(03b1), H(03c3), H(03b4), H(03c6), H(03b3), H(03b7), H(03d1), H(03ba), H(03bb), H(22ef), H(22c5), _______, KC_P4, KC_P5, KC_P6, H(2295),
_______, H(03b6), H(03be), H(03c7), H(03c2), H(03b2), H(03bd), H(03bc), H(226a), H(226b), H(222b), _______, _______, KC_P1, KC_P2, KC_P3,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_P0, KC_PDOT, KC_PENT),
[_SHIFTGREEK] = LAYOUT_hotswap(
KC_GRV, H(2081), H(2082), H(2083), H(2084), H(2085), H(2086), H(2087), H(2088), H(2089), H(2080), H(208b), H(208a), H(208d), H(208e), XXXXXXX, XXXXXXX, XXXXXXX, _______,
KC_GRV, H(00bf), _______, H(20ac), _______, _______, _______, _______, _______, _______, _______, XXXXXXX, H(2241), _______, _______, XXXXXXX, H(2297), XXXXXXX,
_______, H(0398), H(03a9), H(0395), H(03a1), H(03a4), H(03a8), H(03a5), H(0399), H(039f), H(03a0), KC_LBRC, KC_RBRC, KC_BSLS, KC_P7, KC_P8, KC_P9,
_______, H(0391), H(03a3), H(0394), H(03a6), H(0393), H(0397), XXXXXXX, H(039a), H(039b), H(2026), H(2234), _______, KC_P4, KC_P5, KC_P6, H(2295),
_______, H(0396), H(039e), H(03a7), H(2714), H(0392), H(039d), H(039c), H(2272), H(2273), XXXXXXX, _______, _______, KC_P1, KC_P2, KC_P3,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_P0, KC_PDOT, KC_PENT),
[_CADET] = LAYOUT_hotswap(
H(00AC), KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, _______,
KC_GRV, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, H(00b1), _______, _______, XXXXXXX, H(00d7), XXXXXXX,
_______, H(2227), H(2228), H(2229), H(222a), H(2282), H(2283), H(2200), H(221e), H(2203), H(2202), H(2208), XXXXXXX, XXXXXXX, KC_P7, KC_P8, KC_P9,
_______, H(22a5), H(22a4), H(22a2), H(22a3), H(2191), H(2193), H(2190), H(2192), H(2194), XXXXXXX, XXXXXXX, _______, KC_P4, KC_P5, KC_P6, XXXXXXX,
_______, XXXXXXX, H(2260), XXXXXXX, H(2248), H(2261), H(2264), H(2265), XXXXXXX, XXXXXXX, H(00f7), _______, _______, KC_P1, KC_P2, KC_P3,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_P0, KC_PDOT, KC_PENT),
[_SHIFTCADET] = LAYOUT_hotswap(
H(2205), KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, _______,
KC_GRV, H(00a1), XXXXXXX, H(00a3), XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, H(2213), _______, _______, XXXXXXX, XXXXXXX, XXXXXXX,
_______, H(211a), XXXXXXX, XXXXXXX, H(211d), H(2286), H(2287), XXXXXXX, H(2135), H(2204), XXXXXXX, H(2209), XXXXXXX, XXXXXXX, KC_P7, KC_P8, KC_P9,
_______, H(212b), XXXXXXX, H(2207), XXXXXXX, H(21d1), H(21d3), H(21d0), H(21d2), H(21d4), XXXXXXX, XXXXXXX, _______, KC_P4, KC_P5, KC_P6, XXXXXXX,
_______, H(2124), H(2102), XXXXXXX, H(2249), H(2262), H(2115), XXXXXXX, XXXXXXX, XXXXXXX, H(00f7), _______, _______, KC_P1, KC_P2, KC_P3,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_P0, KC_PDOT, KC_PENT),
[_FUNCTION] = LAYOUT_hotswap(
KC_CGRV, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_MUTE, KC_VOLD, KC_VOLU, _______, _______, RESET,
KC_CGRV, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, KC_CAGU, _______, _______, _______, KC_CDIA, KC_CCIR, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, UC_M_OS, UC_M_LN, UC_M_WI, UC_M_BS, UC_M_WC, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, KC_CCED, _______, _______, KC_CTIL, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______),
};
#define KC_NUM_ACCENTS (KC_ACCENT_END - KC_ACCENT_START)
#define KC_NUM_SLOTS (KC_Z + 1)
const uint16_t PROGMEM unshifted_accents[KC_NUM_SLOTS][KC_NUM_ACCENTS] = {
[KC_A] = { 0x00e0, 0x00e1, 0x00e4, 0x00e2, 0, 0x00e3 },
[KC_E] = { 0x00e8, 0x00e9, 0x00eb, 0x00ea, 0, 0 },
[KC_I] = { 0x00ec, 0x00ed, 0x00ef, 0x00ee, 0, 0 },
[KC_O] = { 0x00f2, 0x00f3, 0x00f6, 0x00f4, 0, 0x00f5 },
[KC_U] = { 0x00f9, 0x00fa, 0x00fc, 0x00fb, 0, 0 },
[KC_Y] = { 0, 0, 0x00ff, 0, 0, 0 },
[KC_N] = { 0, 0, 0, 0, 0, 0x00f1 },
[KC_C] = { 0, 0, 0, 0, 0x00e7, 0 },
};
const uint16_t PROGMEM shifted_accents[KC_NUM_SLOTS][KC_NUM_ACCENTS] = {
[KC_A] = { 0x00c0, 0x00c1, 0x00c4, 0x00c2, 0, 0x00c3 },
[KC_E] = { 0x00c8, 0x00c9, 0x00cb, 0x00ca, 0, 0 },
[KC_I] = { 0x00cc, 0x00cd, 0x00cf, 0x00ce, 0, 0 },
[KC_O] = { 0x00d2, 0x00d3, 0x00d6, 0x00d4, 0, 0x00d5 },
[KC_U] = { 0x00d9, 0x00da, 0x00dc, 0x00db, 0, 0 },
[KC_Y] = { 0, 0, 0x00df, 0, 0, 0 },
[KC_N] = { 0, 0, 0, 0, 0, 0x00d1 },
[KC_C] = { 0, 0, 0, 0, 0x00c7, 0 },
};
const uint16_t PROGMEM uncombined_accents[KC_NUM_ACCENTS] = {
[KC_CGRV - KC_ACCENT_START] = 0x0060,
[KC_CAGU - KC_ACCENT_START] = 0x00b4,
[KC_CDIA - KC_ACCENT_START] = 0x00a8,
[KC_CCIR - KC_ACCENT_START] = 0x005e,
[KC_CCED - KC_ACCENT_START] = 0x00b8,
[KC_CTIL - KC_ACCENT_START] = 0x02dc,
};
const uint16_t PROGMEM combined_accents[KC_NUM_ACCENTS] = {
[KC_CGRV - KC_ACCENT_START] = 0x0300,
[KC_CAGU - KC_ACCENT_START] = 0x0301,
[KC_CDIA - KC_ACCENT_START] = 0x0308,
[KC_CCIR - KC_ACCENT_START] = 0x0302,
[KC_CCED - KC_ACCENT_START] = 0x0327,
[KC_CTIL - KC_ACCENT_START] = 0x0303,
};
bool process_key_after_accent(
uint16_t accent_key,
uint16_t keycode,
bool is_shifted,
bool force_no_accent
) {
assert(accent_key >= KC_ACCENT_START);
assert(accent_key < KC_ACCENT_END);
const int accent_index = accent_key - KC_ACCENT_START;
if (keycode <= KC_Z && !force_no_accent) {
const uint16_t *points = (const uint16_t*)(is_shifted ? shifted_accents : unshifted_accents);
const uint16_t code_point = pgm_read_word_near(points + KC_NUM_ACCENTS * keycode + accent_index);
if (code_point) {
register_unicode(code_point);
return true;
}
}
register_unicode(pgm_read_word_near(uncombined_accents + accent_index));
return false;
}
#define GREEK_LAYER (1UL << _GREEK)
#define SHIFTGREEK_LAYER (1UL << _SHIFTGREEK)
#define CADET_LAYER (1UL << _CADET)
#define SHIFTCADET_LAYER (1UL << _SHIFTCADET)
#define LAYER_MASK ~(GREEK_LAYER|SHIFTGREEK_LAYER|CADET_LAYER|SHIFTCADET_LAYER)
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
static bool shift_held = false;
static bool alt_held = false;
static bool ctrl_held = false;
static bool super_held = false;
static bool greek_held = false;
static bool cadet_held = false;
static bool shift_lock = false;
static int layer_lock = _QWERTY;
static uint16_t accent_request = 0;
bool ignore_accent_change = !record->event.pressed;
if (keycode == KC_LSHIFT || keycode == KC_RSHIFT) {
shift_held = record->event.pressed;
ignore_accent_change = true;
} else if (keycode == KC_LALT || keycode == KC_RALT) {
alt_held = record->event.pressed;
ignore_accent_change = true;
} else if (keycode == KC_LCTRL || keycode == KC_RCTRL) {
ctrl_held = record->event.pressed;
ignore_accent_change = true;
} else if (keycode == KC_LGUI || keycode == KC_RGUI) {
super_held = record->event.pressed;
ignore_accent_change = true;
} else if (keycode == KC_GREEK) {
greek_held = record->event.pressed;
ignore_accent_change = true;
} else if (keycode == KC_CADET) {
cadet_held = record->event.pressed;
ignore_accent_change = true;
}
const bool greek_request = (greek_held && !alt_held);
const bool cadet_request = (cadet_held || (greek_held && alt_held));
int next_layer_lock = layer_lock;
if (keycode == KC_CAPS) {
if (record->event.pressed) {
shift_lock = !shift_lock;
}
} else if (keycode == KC_LAYER_LOCK) {
if (record->event.pressed) {
if (cadet_request) {
next_layer_lock = _CADET;
} else if (greek_request) {
next_layer_lock = _GREEK;
} else {
next_layer_lock = _QWERTY;
}
}
}
int base_layer;
if (cadet_request) {
base_layer = (layer_lock == _CADET ? _QWERTY : _CADET);
} else if (greek_request) {
base_layer = (layer_lock == _GREEK ? _QWERTY : _GREEK);
} else {
base_layer = layer_lock;
}
const bool shifted = (shift_held != shift_lock);
int actual_layer;
if (base_layer == _CADET) {
actual_layer = (shifted ? _SHIFTCADET : _CADET);
} else if (base_layer == _GREEK) {
actual_layer = (shifted ? _SHIFTGREEK : _GREEK);
} else {
actual_layer = _QWERTY;
}
layer_lock = next_layer_lock;
layer_state_t new_layer_state = (layer_state & LAYER_MASK) | (1UL << actual_layer);
if (new_layer_state != layer_state) {
layer_state_set(new_layer_state);
}
if (!ignore_accent_change && accent_request && record->event.pressed) {
const bool force_no_accent = (
actual_layer != _QWERTY ||
ctrl_held ||
super_held ||
alt_held
);
const uint16_t old_accent = accent_request;
accent_request = 0;
if (process_key_after_accent(old_accent, keycode, shifted, force_no_accent)) {
return false;
}
}
if (keycode >= KC_ACCENT_START && keycode < KC_ACCENT_END && record->event.pressed) {
if (shifted) {
register_unicode(pgm_read_word_near(combined_accents + keycode - KC_ACCENT_START));
return false;
} else {
accent_request = keycode;
}
}
return true;
}