From 3398d028c61165c219bf9361f5b81d3a54e968c8 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Wed, 28 Dec 2022 00:27:22 -0500 Subject: Use regular expressions for tagging windows by class and name --- .gitignore | 3 +++ config.h | 24 ++++++++++++------------ dwm.c | 6 +++--- util2.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ util2.h | 24 ------------------------ 5 files changed, 71 insertions(+), 39 deletions(-) create mode 100644 .gitignore create mode 100644 util2.c delete mode 100644 util2.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..80baf9b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +tags +dwm diff --git a/config.h b/config.h index abeeaee..f0c2cc7 100644 --- a/config.h +++ b/config.h @@ -37,18 +37,18 @@ static const Rule rules[] = { * WM_NAME(STRING) = title */ /* class instance title tags mask isfloating monitor */ - { "St", NULL, NULL, 1<<0, 0, -1 }, - { "Gimp", NULL, NULL, 1<<2, 0, -1 }, - { "Pavucontrol", NULL, NULL, 1<<6, 0, -1 }, - { "Blueman-manager",NULL, NULL, 1<<6, 0, -1 }, - { "Claws-mail", NULL, NULL, 1<<7, 0, -1 }, - { "Evolution", NULL, NULL, 1<<7, 0, -1 }, - { "Zotero", NULL, NULL, 1<<7, 0, -1 }, - { "Google-chrome", NULL, NULL, 1<<8, 0, -1 }, - { "Chromium", NULL, NULL, 1<<8, 0, -1 }, - { "Firefox", NULL, NULL, 1<<8, 0, -1 }, - { "firefox", NULL, NULL, 1<<8, 0, -1 }, - { "code-oss", NULL, NULL, 1<<1, 0, -1 }, + { "^St$", NULL, NULL, 1<<0, 0, -1 }, + { "^Gimp$", NULL, NULL, 1<<2, 0, -1 }, + { "^Pavucontrol$", NULL, NULL, 1<<6, 0, -1 }, + { "^Blueman-manager$",NULL, NULL, 1<<6, 0, -1 }, + { "^Claws-mail$", NULL, NULL, 1<<7, 0, -1 }, + { "^Evolution$", NULL, NULL, 1<<7, 0, -1 }, + { "^Zotero$", NULL, NULL, 1<<7, 0, -1 }, + { "^Google-chrome$", NULL, NULL, 1<<8, 0, -1 }, + { "^Chromium$", NULL, NULL, 1<<8, 0, -1 }, + { "^Firefox$", NULL, NULL, 1<<8, 0, -1 }, + { "^code-oss$", NULL, NULL, 1<<1, 0, -1 }, + { "^jetbrains.*$", NULL, NULL, 1<<2, 0, -1 }, }; /* layout(s) */ diff --git a/dwm.c b/dwm.c index ee4137f..67955f6 100644 --- a/dwm.c +++ b/dwm.c @@ -43,7 +43,7 @@ #include "drw.h" #include "util.h" -#include "util2.h" +#include "util2.c" /* macros */ #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) @@ -298,8 +298,8 @@ applyrules(Client *c) for (i = 0; i < LENGTH(rules); i++) { r = &rules[i]; - if ((!r->title || strstr(c->name, r->title)) - && (!r->class || strstr(class, r->class)) + if ((!r->title || reMatch(r->title, c->name) == 0) + && (!r->class || reMatch(r->class, class) == 0) && (!r->instance || strstr(instance, r->instance))) { c->isfloating = r->isfloating; diff --git a/util2.c b/util2.c new file mode 100644 index 0000000..1ce2c55 --- /dev/null +++ b/util2.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include + +int +readEnv(FILE *fh) +{ + int ret; + char name[1024], value[8192]; + + while (1) { + if ((ret = fscanf(fh, "%1023[^=]=%8191[^\n]\n", &name, &value)) == EOF) + break; + else if (ret == 0) + break; + + fprintf(stderr, "Setting: '%s' = '%s'\n", name, value); + setenv(name, value, 1); + } + + return 0; +} + +int +reMatch(const char *regex, const char *str) { + char reErr[1024] = {0}; + regex_t *re = calloc(1, sizeof(regex_t)); + int rc = 0; + + if (!re) + return -1; + + rc = regcomp(re, regex, REG_EXTENDED|REG_ICASE|REG_NOSUB); + if (rc != 0) { + regerror(rc, re, reErr, 1024); + fprintf(stderr, "Regex compile err: %s %s\n", regex, reErr); + return -1; + } + + rc = regexec(re, str, 0, NULL, 0); + regfree(re); + if (rc != 0) { + regerror(rc, re, reErr, 1024); + fprintf(stderr, "Regex match error: %s -> %s : %s\n", + regex, str, reErr); + return -1; + } + + return rc; +} diff --git a/util2.h b/util2.h deleted file mode 100644 index ab25acf..0000000 --- a/util2.h +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include -#include - -int -readEnv(FILE *fh) -{ - int ret; - char name[1024], value[8192]; - - while (1) { - if ((ret = fscanf(fh, "%1023[^=]=%8191[^\n]\n", &name, &value)) == EOF) - break; - else if (ret == 0) - break; - - fprintf(stderr, "Setting: '%s' = '%s'\n", name, value); - setenv(name, value, 1); - } - - return 0; -} -- cgit v1.2.3