From ba3ca53aef521467049a58aa766104041f987c5e Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Tue, 22 Aug 2023 11:51:15 +1200 Subject: feat: Made the emacs bind mode function better It now uses a regex to split out the different modifiers and keys. --- keybinds.cpp | 67 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/keybinds.cpp b/keybinds.cpp index 88a43fe..a4a78c0 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -5,13 +5,14 @@ #include #include #include +#include #include "commands.h" #include "error.h" #include "keybinds.h" #include "util.h" -using std::string, std::cout, std::endl; +using std::string; bool Keybind::operator<(const Keybind &o) const { if(key != o.key) @@ -164,39 +165,53 @@ const Keybind KeybindsModule::normalBindMode(string bindString) const Keybind KeybindsModule::emacsBindMode(string bindString) { - std::vector keys = split(bindString, '-'); Keybind bind; bind.modifiers = 0; - for(string key : keys) + + const std::regex keyRegex("^(?:([CMs])-)?(?:([CMs])-)?(?:([CMs])-)?([^\\s]+)$"); + std::smatch keyMatch; + if(std::regex_match(bindString, keyMatch, keyRegex)) { - if(key == "s") - { - bind.modifiers |= Mod4Mask >> 3 * cfg.swapSuperAlt; - } - else if(key == "M") - { - bind.modifiers |= Mod1Mask << 3 * cfg.swapSuperAlt; - } - else if(key == "C") + for (int i = 1; i < 3; i++) { - bind.modifiers |= ControlMask; - } - else - { - if(isUpper(key)) - { - bind.modifiers |= ShiftMask; - } - KeySym s = XStringToKeysym(key.c_str()); - if(s == NoSymbol) + std::ssub_match modifierMatch = keyMatch[i]; + if(modifierMatch.matched) { - throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid!"); + std::string modifier = modifierMatch.str(); + if(modifier == "s") + { + bind.modifiers |= Mod4Mask >> 3 * cfg.swapSuperAlt; + } + else if(modifier == "M") + { + bind.modifiers |= Mod1Mask << 3 * cfg.swapSuperAlt; + } + else if(modifier == "C") + { + bind.modifiers |= ControlMask; + } } - bind.key = XKeysymToKeycode(globals.dpy, s); } } - if(!bind.key) - throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid!"); + KeySym keySym = XStringToKeysym(keyMatch[4].str().c_str()); + + if(isUpper(keyMatch[4].str().c_str()) && keySym != NoSymbol) + { + bind.modifiers |= ShiftMask; + } + if(keySym == NoSymbol) + { + if(keyMatch[4].str() == "RET") + keySym = XK_Return; + if(keyMatch[4].str() == "ESC") + keySym = XK_Escape; + if(keyMatch[4].str() == "SPC") + keySym = XK_space; + else + throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid"); + } + bind.key = XKeysymToKeycode(globals.dpy, keySym); + return bind; } -- cgit v1.2.3