switched to regexp matching for Rules

This commit is contained in:
arg@10ksloc.org 2006-08-04 14:40:32 +02:00
parent f504aea132
commit e21d93b7bd
6 changed files with 65 additions and 31 deletions

View file

@ -52,11 +52,10 @@ static Key key[] = { \
}; };
#define RULES \ #define RULES \
const unsigned int two[] = { 2 }; \
static Rule rule[] = { \ static Rule rule[] = { \
/* class:instance tags isfloat */ \ /* class:instance regex tags regex isfloat */ \
{ "Firefox.*", two, False }, \ { "Firefox.*", "net", False }, \
{ "Gimp.*", NULL, True}, \ { "Gimp.*", NULL, True}, \
{ "MPlayer.*", NULL, True}, \ { "MPlayer.*", NULL, True}, \
{ "Acroread.*", NULL, True}, \ { "Acroread.*", NULL, True}, \
}; };

View file

@ -47,9 +47,8 @@ static Key key[] = { \
}; };
#define RULES \ #define RULES \
const unsigned int two[] = { 2 }; \
static Rule rule[] = { \ static Rule rule[] = { \
/* class:instance tags isfloat */ \ /* class:instance regex tags regex isfloat */ \
{ "Firefox.*", two, False }, \ { "Firefox.*", "2", False }, \
{ "Gimp.*", NULL, True}, \ { "Gimp.*", NULL, True}, \
}; };

View file

@ -15,10 +15,10 @@ INCS = -I/usr/lib -I${X11INC}
LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 LIBS = -L/usr/lib -lc -L${X11LIB} -lX11
# flags # flags
CFLAGS = -O3 ${INCS} -DVERSION=\"${VERSION}\" #CFLAGS = -O3 ${INCS} -DVERSION=\"${VERSION}\"
LDFLAGS = ${LIBS} #LDFLAGS = ${LIBS}
#CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\" CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\"
#LDFLAGS = -g ${LIBS} LDFLAGS = -g ${LIBS}
# compiler # compiler
CC = cc CC = cc

1
dwm.h
View file

@ -121,6 +121,7 @@ extern int xerror(Display *dsply, XErrorEvent *ee);
extern void appendtag(Arg *arg); extern void appendtag(Arg *arg);
extern void dofloat(Arg *arg); extern void dofloat(Arg *arg);
extern void dotile(Arg *arg); extern void dotile(Arg *arg);
extern void initrregs();
extern Client *getnext(Client *c); extern Client *getnext(Client *c);
extern Client *getprev(Client *c); extern Client *getprev(Client *c);
extern void replacetag(Arg *arg); extern void replacetag(Arg *arg);

1
main.c
View file

@ -210,6 +210,7 @@ main(int argc, char *argv[])
cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
grabkeys(); grabkeys();
initrregs();
for(ntags = 0; tags[ntags]; ntags++); for(ntags = 0; tags[ntags]; ntags++);

68
tag.c
View file

@ -5,21 +5,31 @@
#include "dwm.h" #include "dwm.h"
#include <regex.h> #include <regex.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>
/* static */
typedef struct { typedef struct {
const char *pattern; const char *clpattern;
const unsigned int *tags; const char *tpattern;
Bool isfloat; Bool isfloat;
} Rule; } Rule;
typedef struct {
regex_t *clregex;
regex_t *tregex;
} RReg;
/* static */
TAGS TAGS
RULES RULES
static RReg *rreg = NULL;
static unsigned int len = 0;
void (*arrange)(Arg *) = DEFMODE; void (*arrange)(Arg *) = DEFMODE;
/* extern */ /* extern */
@ -137,6 +147,35 @@ getprev(Client *c)
return c; return c;
} }
void
initrregs()
{
unsigned int i;
regex_t *reg;
if(rreg)
return;
len = sizeof(rule) / sizeof(rule[0]);
rreg = emallocz(len * sizeof(RReg));
for(i = 0; i < len; i++) {
if(rule[i].clpattern) {
reg = emallocz(sizeof(regex_t));
if(regcomp(reg, rule[i].clpattern, 0))
free(reg);
else
rreg[i].clregex = reg;
}
if(rule[i].tpattern) {
reg = emallocz(sizeof(regex_t));
if(regcomp(reg, rule[i].tpattern, 0))
free(reg);
else
rreg[i].tregex = reg;
}
}
}
void void
replacetag(Arg *arg) replacetag(Arg *arg)
{ {
@ -154,9 +193,7 @@ void
settags(Client *c) settags(Client *c)
{ {
char classinst[256]; char classinst[256];
static unsigned int len = sizeof(rule) / sizeof(rule[0]); unsigned int i, j;
unsigned int i, j, n;
regex_t regex;
regmatch_t tmp; regmatch_t tmp;
Bool matched = False; Bool matched = False;
XClassHint ch; XClassHint ch;
@ -165,19 +202,16 @@ settags(Client *c)
snprintf(classinst, sizeof(classinst), "%s:%s", snprintf(classinst, sizeof(classinst), "%s:%s",
ch.res_class ? ch.res_class : "", ch.res_class ? ch.res_class : "",
ch.res_name ? ch.res_name : ""); ch.res_name ? ch.res_name : "");
for(i = 0; !matched && i < len; i++) { for(i = 0; !matched && i < len; i++)
if(!regcomp(&regex, rule[i].pattern, 0)) { if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
if(!regexec(&regex, classinst, 1, &tmp, 0)) { c->isfloat = rule[i].isfloat;
n = rule[i].tags ? for(j = 0; rreg[i].tregex && j < ntags; j++) {
sizeof(rule[i].tags) / sizeof(rule[i].tags[0]) : 0; if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
matched = n != 0; matched = True;
for(j = 0; j < n; j++) c->tags[j] = True;
c->tags[rule[i].tags[j]] = True; }
c->isfloat = rule[i].isfloat;
} }
regfree(&regex);
} }
}
if(ch.res_class) if(ch.res_class)
XFree(ch.res_class); XFree(ch.res_class);
if(ch.res_name) if(ch.res_name)