summaryrefslogtreecommitdiff
path: root/keybinds.cpp
diff options
context:
space:
mode:
authorBossCode45 <human.cyborg42@gmail.com>2023-08-22 11:51:15 +1200
committerBossCode45 <human.cyborg42@gmail.com>2023-08-22 11:51:15 +1200
commitba3ca53aef521467049a58aa766104041f987c5e (patch)
tree2ec973d9fb1f80f09a820718045b05857c40b1cc /keybinds.cpp
parentea569d9c9c61eb26f7d325b41d8ac839dc470eec (diff)
downloadYATwm-ba3ca53aef521467049a58aa766104041f987c5e.tar.gz
YATwm-ba3ca53aef521467049a58aa766104041f987c5e.zip
feat: Made the emacs bind mode function better
It now uses a regex to split out the different modifiers and keys.
Diffstat (limited to 'keybinds.cpp')
-rw-r--r--keybinds.cpp67
1 files 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 <sstream>
#include <utility>
#include <vector>
+#include <regex>
#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<string> 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;
}