diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands.cpp | 43 | ||||
| -rw-r--r-- | src/commands.h | 2 | ||||
| -rw-r--r-- | src/config.cpp | 2 | ||||
| -rw-r--r-- | src/config.h | 2 | ||||
| -rw-r--r-- | src/keybinds.cpp | 55 | ||||
| -rw-r--r-- | src/main.cpp | 30 |
6 files changed, 94 insertions, 40 deletions
diff --git a/src/commands.cpp b/src/commands.cpp index 5688da4..caa82eb 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -3,15 +3,14 @@ #include "util.h" #include <cctype> +#include <filesystem> #include <iostream> #include <algorithm> -#include <iterator> #include <stdexcept> #include <string> -#include <utility> #include <vector> -#include <regex> #include <cstring> +#include <fstream> using std::cout, std::endl, std::string, std::vector; @@ -20,9 +19,47 @@ const void CommandsModule::echo(const CommandArg* argv) cout << argv[0].str << endl; } +const void CommandsModule::loadFile(const CommandArg* argv) +{ + std::vector<Err> errs; + + string path = cwd + "/" + std::string(argv[0].str); + + std::ifstream file(path); + if(!file.good()) + throw Err(CMD_ERR_NON_FATAL, "File '" + std::string(argv[0].str) + "' doesn't exist"); + + string prevCWD = cwd; + cwd = std::filesystem::path(path).parent_path(); + + cout << "Loading file '" << argv[0].str << "'" << endl; + + int line = 0; + for(string cmd; std::getline(file, cmd);) + { + line++; + if(cmd.size() == 0) + continue; + if(cmd.at(0) == '#') + continue; + try + { + this->runCommand(cmd); + } + catch (Err e) + { + throw Err(e.code, "Error in file '" + std::string(argv[0].str) + "' (line " + std::to_string(line) + "): " + std::to_string(e.code) + "\n\tMessage: " + e.message); + } + } + + cwd = prevCWD; +} + CommandsModule::CommandsModule() { addCommand("echo", &CommandsModule::echo, 1, {STR_REST}, this); + addCommand("loadFile", &CommandsModule::loadFile, 1, {STR_REST}, this); + cwd = std::filesystem::current_path(); } CommandsModule::~CommandsModule() { diff --git a/src/commands.h b/src/commands.h index af4a4a0..4825f1c 100644 --- a/src/commands.h +++ b/src/commands.h @@ -52,6 +52,8 @@ private: std::vector<std::string> splitCommand(std::string command); CommandArg* getCommandArgs(std::vector<std::string>& args, const CommandArgType* argTypes, const int argc); const void echo(const CommandArg* argv); + const void loadFile(const CommandArg* argv); + std::string cwd; public: CommandsModule(); ~CommandsModule(); diff --git a/src/config.cpp b/src/config.cpp index 925e6ea..df8a43d 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -57,7 +57,7 @@ Config::Config(CommandsModule& commandsModule) commandsModule.addCommand("gaps", &Config::gapsCmd, 1, {NUM}, this); commandsModule.addCommand("outergaps", &Config::outerGapsCmd, 1, {NUM}, this); commandsModule.addCommand("logfile", &Config::logFileCmd, 1, {STR_REST}, this); - commandsModule.addCommand("addworkspace", &Config::addWorkspaceCmd, 2, {STR, NUM_ARR_REST}, this); + commandsModule.addCommand("addWorkspace", &Config::addWorkspaceCmd, 2, {STR, NUM_ARR_REST}, this); commandsModule.addCommand("swapmods", &Config::swapSuperAltCmd, 0, {}, this); } diff --git a/src/config.h b/src/config.h index 452db9c..55e4fd3 100644 --- a/src/config.h +++ b/src/config.h @@ -1,8 +1,8 @@ #pragma once -#include "commands.h" #include <X11/X.h> #include <X11/keysym.h> +#include "commands.h" #include <string> #include <vector> diff --git a/src/keybinds.cpp b/src/keybinds.cpp index 235f8c1..1f60a75 100644 --- a/src/keybinds.cpp +++ b/src/keybinds.cpp @@ -168,48 +168,55 @@ const Keybind KeybindsModule::emacsBindMode(string bindString) Keybind bind; bind.modifiers = 0; - const std::regex keyRegex("^(?:([CMs])-)?(?:([CMs])-)?(?:([CMs])-)?([^\\s]|(SPC|ESC|RET|))$"); + //cout << "Adding keybind: '" << bindString << "'" << endl; + + const std::regex keyRegex("^((?:[CMSs]-)*)([^\\s]|(?:SPC|ESC|RET))$"); std::smatch keyMatch; if(std::regex_match(bindString, keyMatch, keyRegex)) { - for (int i = 1; i < 3; i++) + std::ssub_match modifierMatch = keyMatch[1]; + for(std::string modifier : split(modifierMatch, '-')) { - std::ssub_match modifierMatch = keyMatch[i]; - if(modifierMatch.matched) + if(modifier == "s") + { + //cout << "\tModifier: s" << endl; + bind.modifiers |= cfg.swapSuperAlt?Mod1Mask:Mod4Mask; + } + else if(modifier == "M") + { + //cout << "\tModifier: M" << endl; + bind.modifiers |= cfg.swapSuperAlt?Mod4Mask:Mod1Mask; + } + else if(modifier == "C") + { + //cout << "\tModifier: C" << endl; + bind.modifiers |= ControlMask; + } + else if(modifier == "S") { - 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; - } + //cout << "\tModifier: S" << endl; + bind.modifiers |= ShiftMask; } } } - KeySym keySym = XStringToKeysym(keyMatch[4].str().c_str()); + KeySym keySym = XStringToKeysym(keyMatch[2].str().c_str()); + //cout << "\tKey: " << keyMatch[2].str() << endl; - if(isUpper(keyMatch[4].str().c_str()) && keySym != NoSymbol) + if(isUpper(keyMatch[2].str().c_str()) && keySym != NoSymbol) { bind.modifiers |= ShiftMask; } if(keySym == NoSymbol) { - if(keyMatch[4].str() == "RET") + if(keyMatch[2].str() == "RET") keySym = XK_Return; - else if(keyMatch[4].str() == "ESC") + else if(keyMatch[2].str() == "ESC") keySym = XK_Escape; - else if(keyMatch[4].str() == "SPC") + else if(keyMatch[2].str() == "SPC") keySym = XK_space; - else if(keyMatch[4].str() == "-") + else if(keyMatch[2].str() == "-") keySym = XK_minus; - else if(keyMatch[4].str() == "+") + else if(keyMatch[2].str() == "+") keySym = XK_plus; else throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid"); diff --git a/src/main.cpp b/src/main.cpp index da1ae9b..45e4eba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,6 @@ #include <X11/Xutil.h> #include <X11/extensions/Xrandr.h> -#include <bits/getopt_core.h> #include <libnotify/notification.h> #include <libnotify/notify.h> @@ -18,6 +17,7 @@ #include <iostream> #include <map> #include <ostream> +#include <regex> #include <string> #include <sys/poll.h> #include <sys/select.h> @@ -1029,13 +1029,15 @@ int main(int argc, char** argv) { int versionFlag = 0; bool immediateExit = false; + string configLocation = ""; while(1) { static option long_options[] = {{"version", no_argument, &versionFlag, 1}, + {"config", required_argument, NULL, 'c'}, {0, 0, 0, 0}}; int optionIndex; - char c = getopt_long(argc, argv, "v", long_options, &optionIndex); + char c = getopt_long(argc, argv, "c:v", long_options, &optionIndex); if(c == -1) break; @@ -1047,8 +1049,12 @@ int main(int argc, char** argv) break; //Option had arg break; + case 'c': + configLocation = string(optarg); + break; case 'v': versionFlag = 1; + break; case '?': //Error?? break; @@ -1123,16 +1129,19 @@ int main(int argc, char** argv) std::vector<Err> cfgErr; cout << "Registered commands" << endl; - - char* confDir = getenv("XDG_CONFIG_HOME"); - if(confDir != NULL) - { - cfgErr = cfg.loadFromFile(string(confDir) + "/YATwm/config"); - } + + if(configLocation != "") + cfgErr = cfg.loadFromFile(configLocation); else { - string home = getenv("HOME"); - cfgErr = cfg.loadFromFile(home + "/.config/YATwm/config"); + char* confDir = getenv("XDG_CONFIG_HOME"); + if(confDir != NULL) + cfgErr = cfg.loadFromFile(string(confDir) + "/YATwm/config"); + else + { + string home = getenv("HOME"); + cfgErr = cfg.loadFromFile(home + "/.config/YATwm/config"); + } } cout << "Done config" << endl; @@ -1233,7 +1242,6 @@ int main(int argc, char** argv) } if(ready == -1) { - cout << "E" << endl; log("ERROR"); } } |
