Skip to content

Instantly share code, notes, and snippets.

@dougmarcey
Created January 22, 2019 21:02
Show Gist options
  • Save dougmarcey/547ad1cd474b167f765f223c717e1075 to your computer and use it in GitHub Desktop.
Save dougmarcey/547ad1cd474b167f765f223c717e1075 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Tool to convert Ergodox-EZ Configurator generated keymaps
# to somthing a little easier to read/work with.
#
# runs like:
# cleanup.rb [downloaded keymap.c] > [destination keymap.c]
open(FILE,$ARGV[0]) || die "Can't open the file [$!]\n";
local $/;
$keymap = <FILE>;
close(FILE);
$keymap =~ s/LALT\(LCTL\(LGUI\(LSFT\((.*?)\)\)\)\)/HYPR($1)/g;
$keymap =~ s/LGUI\(LSFT\((.*?)\)\)/SGUI($1)/g;
$keymap =~ s/LALT\(LCTL\((.*?)\)\)/LCA($1)/g;
$keymap =~ s/LCTL\(LGUI\(LSFT\((.*?)\)\)\)/LCGS($1)/g;
$keymap =~ s/LCTL\(LSFT\((.*?)\)\)/LCS($1)/g;
$keymap =~ s/MT\(MOD_HYPR, (.*?)\)/HYPR_T($1)/g;
$keymap =~ s/MT\(MOD_MEH, (.*?)\)/MEH_T($1)/g;
$keymap =~ s/KC_TRANSPARENT/_______/g;
$keymap =~ s/KC_TRANSPARENT/XXXXXXX/g;
$keymap =~ s/KC_NO/__X__/g;
$keymap =~ s/KC_ENTER/KC_ENT/g;
$keymap =~ s/KC_ESCAPE/KC_ESC/g;
$keymap =~ s/KC_BSPACE/KC_BSPC/g;
$keymap =~ s/KC_SPACE/KC_SPC/g;
$keymap =~ s/KC_MINUS/KC_MINS/g;
$keymap =~ s/KC_EQUAL/KC_EQL/g;
$keymap =~ s/KC_LBRACKET/KC_LBRC/g;
$keymap =~ s/KC_RBRACKET/KC_RBRC/g;
$keymap =~ s/KC_BSLASH/KC_BSLS/g;
$keymap =~ s/KC_NONUS_HASH/KC_NUHS/g;
$keymap =~ s/KC_SCOLON/KC_SCLN/g;
$keymap =~ s/KC_QUOTE/KC_QUOT/g;
$keymap =~ s/KC_GRAVE/KC_GRV/g;
$keymap =~ s/KC_COMMA/KC_COMM/g;
$keymap =~ s/KC_SLASH/KC_SLSH/g;
$keymap =~ s/KC_NONUS_BSLASH/KC_NUBS/g;
$keymap =~ s/KC_CAPSLOCK/KC_CLCK/g;
$keymap =~ s/KC_CAPSLOCK/KC_CAPS/g;
$keymap =~ s/KC_SCROLLLOCK/KC_SLCK/g;
$keymap =~ s/KC_NUMLOCK/KC_NLCK/g;
$keymap =~ s/KC_LOCKING_CAPS/KC_LCAP/g;
$keymap =~ s/KC_LOCKING_NUM/KC_LNUM/g;
$keymap =~ s/KC_LOCKING_SCROLL/KC_LSCR/g;
$keymap =~ s/KC_PSCREEN/KC_PSCR/g;
$keymap =~ s/KC_PAUSE/KC_PAUS/g;
$keymap =~ s/KC_PAUSE/KC_BRK/g;
$keymap =~ s/KC_INSERT/KC_INS/g;
$keymap =~ s/KC_DELETE/KC_DEL/g;
$keymap =~ s/KC_PGDOWN/KC_PGDN/g;
$keymap =~ s/KC_RIGHT/KC_RGHT/g;
$keymap =~ s/KC_APPLICATION/KC_APP/g;
$keymap =~ s/KC_EXECUTE/KC_EXEC/g;
$keymap =~ s/KC_SELECT/KC_SLCT/g;
$keymap =~ s/KC_AGAIN/KC_AGIN/g;
$keymap =~ s/KC_PASTE/KC_PSTE/g;
$keymap =~ s/KC_ALT_ERASE/KC_ERAS/g;
$keymap =~ s/KC_CLEAR/KC_CLR/g;
$keymap =~ s/KC_KP_SLASH/KC_PSLS/g;
$keymap =~ s/KC_KP_ASTERISK/KC_PAST/g;
$keymap =~ s/KC_KP_MINUS/KC_PMNS/g;
$keymap =~ s/KC_KP_PLUS/KC_PPLS/g;
$keymap =~ s/KC_KP_ENTER/KC_PENT/g;
$keymap =~ s/KC_KP_1/KC_P1/g;
$keymap =~ s/KC_KP_2/KC_P2/g;
$keymap =~ s/KC_KP_3/KC_P3/g;
$keymap =~ s/KC_KP_4/KC_P4/g;
$keymap =~ s/KC_KP_5/KC_P5/g;
$keymap =~ s/KC_KP_6/KC_P6/g;
$keymap =~ s/KC_KP_7/KC_P7/g;
$keymap =~ s/KC_KP_8/KC_P8/g;
$keymap =~ s/KC_KP_9/KC_P9/g;
$keymap =~ s/KC_KP_0/KC_P0/g;
$keymap =~ s/KC_KP_DOT/KC_PDOT/g;
$keymap =~ s/KC_KP_EQUAL/KC_PEQL/g;
$keymap =~ s/KC_KP_COMMA/KC_PCMM/g;
$keymap =~ s/KC_GRAVE/KC_ZKHK/g;
$keymap =~ s/KC_INT1/KC_RO/g;
$keymap =~ s/KC_INT2/KC_KANA/g;
$keymap =~ s/KC_INT3/KC_JYEN/g;
$keymap =~ s/KC_INT4/KC_HENK/g;
$keymap =~ s/KC_INT5/KC_MHEN/g;
$keymap =~ s/KC_LANG1/KC_HAEN/g;
$keymap =~ s/KC_LANG2/KC_HANJ/g;
$keymap =~ s/KC_LCTRL/KC_LCTL/g;
$keymap =~ s/KC_LSHIFT/KC_LSFT/g;
$keymap =~ s/KC_LGUI/KC_LCMD/g;
$keymap =~ s/KC_LGUI/KC_LWIN/g;
$keymap =~ s/KC_RCTRL/KC_RCTL/g;
$keymap =~ s/KC_RSHIFT/KC_RSFT/g;
$keymap =~ s/KC_RALT/KC_ALGR/g;
$keymap =~ s/KC_RGUI/KC_RCMD/g;
$keymap =~ s/KC_RGUI/KC_RWIN/g;
$keymap =~ s/KC_SYSTEM_POWER/KC_PWR/g;
$keymap =~ s/KC_SYSTEM_SLEEP/KC_SLEP/g;
$keymap =~ s/KC_SYSTEM_WAKE/KC_WAKE/g;
$keymap =~ s/KC_AUDIO_MUTE/KC_MUTE/g;
$keymap =~ s/KC_AUDIO_VOL_UP/KC_VOLU/g;
$keymap =~ s/KC_AUDIO_VOL_DOWN/KC_VOLD/g;
$keymap =~ s/KC_MEDIA_NEXT_TRACK/KC_MNXT/g;
$keymap =~ s/KC_MEDIA_PREV_TRACK/KC_MPRV/g;
$keymap =~ s/KC_MEDIA_STOP/KC_MSTP/g;
$keymap =~ s/KC_MEDIA_PLAY_PAUSE/KC_MPLY/g;
$keymap =~ s/KC_MEDIA_SELECT/KC_MSEL/g;
$keymap =~ s/KC_MEDIA_EJECT/KC_EJCT/g;
$keymap =~ s/KC_MAIL/KC_MAIL/g;
$keymap =~ s/KC_CALCULATOR/KC_CALC/g;
$keymap =~ s/KC_MY_COMPUTER/KC_MYCM/g;
$keymap =~ s/KC_WWW_SEARCH/KC_WSCH/g;
$keymap =~ s/KC_WWW_HOME/KC_WHOM/g;
$keymap =~ s/KC_WWW_BACK/KC_WBAK/g;
$keymap =~ s/KC_WWW_FORWARD/KC_WFWD/g;
$keymap =~ s/KC_WWW_STOP/KC_WSTP/g;
$keymap =~ s/KC_WWW_REFRESH/KC_WREF/g;
$keymap =~ s/KC_WWW_FAVORITES/KC_WFAV/g;
$keymap =~ s/KC_MEDIA_FAST_FORWARD/KC_MFFD/g;
$keymap =~ s/KC_MEDIA_REWIND/KC_MRWD/g;
$keymap =~ s/KC_BRIGHTNESS_UP/KC_BRIU/g;
$keymap =~ s/KC_BRIGHTNESS_DOWN/KC_BRID/g;
$keymap =~ s/KC_PAUSE/KC_BRMU/g;
$keymap =~ s/KC_SCROLLLOCK/KC_BRMD/g;
$keymap =~ s/KC_MS_UP/KC_MS_U/g;
$keymap =~ s/KC_MS_DOWN/KC_MS_D/g;
$keymap =~ s/KC_MS_LEFT/KC_MS_L/g;
$keymap =~ s/KC_MS_RIGHT/KC_MS_R/g;
$keymap =~ s/KC_MS_BTN1/KC_BTN1/g;
$keymap =~ s/KC_MS_BTN2/KC_BTN2/g;
$keymap =~ s/KC_MS_BTN3/KC_BTN3/g;
$keymap =~ s/KC_MS_BTN4/KC_BTN4/g;
$keymap =~ s/KC_MS_BTN5/KC_BTN5/g;
$keymap =~ s/KC_MS_WH_UP/KC_WH_U/g;
$keymap =~ s/KC_MS_WH_DOWN/KC_WH_D/g;
$keymap =~ s/KC_MS_WH_LEFT/KC_WH_L/g;
$keymap =~ s/KC_MS_WH_RIGHT/KC_WH_R/g;
$keymap =~ s/KC_MS_ACCEL0/KC_ACL0/g;
$keymap =~ s/KC_MS_ACCEL1/KC_ACL1/g;
$keymap =~ s/KC_MS_ACCEL2/KC_ACL2/g;
$defines = <<'END_DEFS';
#define LCGS(code) LCTL(LGUI(LSFT(code)))
#define LCS(code) LCTL(LSFT(code))
END_DEFS
@keys = $keymap =~ /[A-Z][A-Z0-9_]*?(?:\(.*?\))?,/smg;
@sorted_keys = sort { length $b <=> length $a } @keys;
$size = (length $sorted_keys[0]) + 1;
print STDERR $sorted_keys[0],": ",$size,"\n";
$pad = " " x $size;
$pad8 = $pad x 4;
$pad6 = $pad8 . $pad;
$pad7 = $pad6 . $pad;
$dent = " " x 4;
$ddnt = $dent x 2;
$keymap =~ s/LAYOUT_ergodox\(
((?:[A-Z0-9_]*?(?:\(.*?\))?,){7})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){7})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){6})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){7})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){5})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){2})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){1})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){3})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){7})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){7})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){6})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){7})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){5})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){2})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){1})
((?:[A-Z0-9_]*?(?:\(.*?\))?,){2})
([A-Z0-9_]*?(?:\(.*?\))?)
\),/LAYOUT_ergodox(
\/\/ left hand
$dent$1
$dent$2
$dent$3
$dent$4
$dent$5
$dent$pad6$6
$dent$pad7$7
$dent$pad8$8
\/\/ right hand
$ddnt$9
$ddnt$10
$ddnt$pad$11
$ddnt$12
$ddnt$13
$ddnt$14
$ddnt$15
$ddnt$16$17
$dent),/gx;
$keymap =~ s{([A-Z_][A-Z0-9_]*?(?:\(.*?\))?,)}{ sprintf "%-${size}s", $1}ge;
$keymap =~ s/(enum custom_keycodes)/$defines\n$1/;
print $keymap;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment