X-Git-Url: https://git.xinqibao.xyz/dwm.git/blobdiff_plain/79cd408844c62963aa0eec45bb0414ed66f06f6f..e70139428aec871756dec03d6901478dc7c0cd39:/tag.c

diff --git a/tag.c b/tag.c
index c9eec90..d121c78 100644
--- a/tag.c
+++ b/tag.c
@@ -1,206 +1,154 @@
-/*
- * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
+/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  * See LICENSE file for license details.
  */
 #include "dwm.h"
-
-#include <string.h>
+#include <regex.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <X11/Xutil.h>
 
 /* static */
 
-/* CUSTOMIZE */ 
-static Rule rule[] = {
-	/* class			instance	tags						isfloat */
-	{ "Firefox-bin",	"firefox-bin",	{ [Twww] = "www" },			False },
-};
+typedef struct {
+	const char *prop;
+	const char *tags;
+	Bool isfloating;
+} Rule;
 
-/* extern */
+typedef struct {
+	regex_t *propregex;
+	regex_t *tagregex;
+} Regs;
 
-/* CUSTOMIZE */
-char *tags[TLast] = {
-	[Tscratch] = "scratch",
-	[Tdev] = "dev",
-	[Twww] = "www",
-	[Twork] = "work",
-};
-void (*arrange)(Arg *) = dotile;
+TAGS
+RULES
 
-void
-appendtag(Arg *arg)
-{
-	if(!sel)
-		return;
+static Regs *regs = NULL;
+static unsigned int nrules = 0;
 
-	sel->tags[arg->i] = tags[arg->i];
-	arrange(NULL);
-}
+/* extern */
 
 void
-dofloat(Arg *arg)
-{
-	Client *c;
-
-	arrange = dofloat;
-	for(c = clients; c; c = c->next) {
-		setgeom(c);
-		if(c->tags[tsel]) {
-			resize(c, True, TopLeft);
+compileregs(void) {
+	unsigned int i;
+	regex_t *reg;
+
+	if(regs)
+		return;
+	nrules = sizeof rule / sizeof rule[0];
+	regs = emallocz(nrules * sizeof(Regs));
+	for(i = 0; i < nrules; i++) {
+		if(rule[i].prop) {
+			reg = emallocz(sizeof(regex_t));
+			if(regcomp(reg, rule[i].prop, REG_EXTENDED))
+				free(reg);
+			else
+				regs[i].propregex = reg;
 		}
-		else
-			ban(c);
-	}
-	if(sel && !sel->tags[tsel]) {
-		if((sel = getnext(clients, tsel))) {
-			higher(sel);
-			focus(sel);
+		if(rule[i].tags) {
+			reg = emallocz(sizeof(regex_t));
+			if(regcomp(reg, rule[i].tags, REG_EXTENDED))
+				free(reg);
+			else
+				regs[i].tagregex = reg;
 		}
 	}
-	drawall();
+}
+
+Bool
+isvisible(Client *c) {
+	unsigned int i;
+
+	for(i = 0; i < ntags; i++)
+		if(c->tags[i] && seltag[i])
+			return True;
+	return False;
 }
 
 void
-dotile(Arg *arg)
-{
-	Client *c;
-	int n, i, w, h;
-
-	w = sw - mw;
-	arrange = dotile;
-	for(n = 0, c = clients; c; c = c->next)
-		if(c->tags[tsel] && !c->isfloat)
-			n++;
-
-	if(n > 1)
-		h = (sh - bh) / (n - 1);
-	else
-		h = sh - bh;
-
-	for(i = 0, c = clients; c; c = c->next) {
-		setgeom(c);
-		if(c->tags[tsel]) {
-			if(c->isfloat) {
-				higher(c);
-				resize(c, True, TopLeft);
-				continue;
-			}
-			if(n == 1) {
-				*c->x = sx;
-				*c->y = sy + bh;
-				*c->w = sw - 2 * c->border;
-				*c->h = sh - 2 * c->border - bh;
-			}
-			else if(i == 0) {
-				*c->x = sx;
-				*c->y = sy + bh;
-				*c->w = mw - 2 * c->border;
-				*c->h = sh - 2 * c->border - bh;
-			}
-			else if(h > bh) {
-				*c->x = sx + mw;
-				*c->y = sy + (i - 1) * h + bh;
-				*c->w = w - 2 * c->border;
-				*c->h = h - 2 * c->border;
-			}
-			else { /* fallback if h < bh */
-				*c->x = sx + mw;
-				*c->y = sy + bh;
-				*c->w = w - 2 * c->border;
-				*c->h = sh - 2 * c->border - bh;
+settags(Client *c, Client *trans) {
+	char prop[512];
+	unsigned int i, j;
+	regmatch_t tmp;
+	Bool matched = trans != NULL;
+	XClassHint ch = { 0 };
+
+	if(matched)
+		for(i = 0; i < ntags; i++)
+			c->tags[i] = trans->tags[i];
+	else {
+		XGetClassHint(dpy, c->win, &ch);
+		snprintf(prop, sizeof prop, "%s:%s:%s",
+				ch.res_class ? ch.res_class : "",
+				ch.res_name ? ch.res_name : "", c->name);
+		for(i = 0; i < nrules; i++)
+			if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
+				c->isfloating = rule[i].isfloating;
+				for(j = 0; regs[i].tagregex && j < ntags; j++) {
+					if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
+						matched = True;
+						c->tags[j] = True;
+					}
+				}
 			}
-			resize(c, False, TopLeft);
-			i++;
-		}
-		else
-			ban(c);
-	}
-	if(!sel || (sel && !sel->tags[tsel])) {
-		if((sel = getnext(clients, tsel))) {
-			higher(sel);
-			focus(sel);
-		}
+		if(ch.res_class)
+			XFree(ch.res_class);
+		if(ch.res_name)
+			XFree(ch.res_name);
 	}
-	drawall();
-}
-
-Client *
-getnext(Client *c, unsigned int t)
-{
-	for(; c && !c->tags[t]; c = c->next);
-	return c;
+	if(!matched)
+		for(i = 0; i < ntags; i++)
+			c->tags[i] = seltag[i];
 }
 
 void
-heretag(Arg *arg)
-{
+tag(const char *arg) {
 	int i;
-	Client *c;
 
-	if(arg->i == tsel)
-		return;
-
-	if(!(c = getnext(clients, arg->i)))
+	if(!sel)
 		return;
-
-	for(i = 0; i < TLast; i++)
-		c->tags[i] = NULL;
-	c->tags[tsel] = tags[tsel];
-	pop(c);
-	focus(c);
+	for(i = 0; i < ntags; i++)
+		sel->tags[i] = arg == NULL;
+	i = arg ? atoi(arg) : 0;
+	if(i >= 0 && i < ntags)
+		sel->tags[i] = True;
+	lt->arrange();
 }
 
 void
-replacetag(Arg *arg)
-{
-	int i;
+toggletag(const char *arg) {
+	int i, j;
+
 	if(!sel)
 		return;
-
-	for(i = 0; i < TLast; i++)
-		sel->tags[i] = NULL;
-	appendtag(arg);
+	i = arg ? atoi(arg) : 0;
+	sel->tags[i] = !sel->tags[i];
+	for(j = 0; j < ntags && !sel->tags[j]; j++);
+	if(j == ntags)
+		sel->tags[i] = True;
+	lt->arrange();
 }
 
 void
-settags(Client *c)
-{
-	XClassHint ch;
-	static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
-	unsigned int i, j;
-	Bool matched = False;
-
-	if(!len) {
-		c->tags[tsel] = tags[tsel];
-		return;
-	}
-
-	if(XGetClassHint(dpy, c->win, &ch)) {
-		if(ch.res_class && ch.res_name) {
-			for(i = 0; i < len; i++)
-				if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
-					&& !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
-				{
-					for(j = 0; j < TLast; j++)
-						c->tags[j] = rule[i].tags[j];
-					c->isfloat = rule[i].isfloat;
-					matched = True;
-					break;
-				}
-		}
-		if(ch.res_class)
-			XFree(ch.res_class);
-		if(ch.res_name)
-			XFree(ch.res_name);
-	}
-
-	if(!matched)
-		c->tags[tsel] = tags[tsel];
+toggleview(const char *arg) {
+	int i, j;
+
+	i = arg ? atoi(arg) : 0;
+	seltag[i] = !seltag[i];
+	for(j = 0; j < ntags && !seltag[j]; j++);
+	if(j == ntags)
+		seltag[i] = True; /* cannot toggle last view */
+	lt->arrange();
 }
 
 void
-view(Arg *arg)
-{
-	tsel = arg->i;
-	arrange(NULL);
-	drawall();
+view(const char *arg) {
+	int i;
+
+	for(i = 0; i < ntags; i++)
+		seltag[i] = arg == NULL;
+	i = arg ? atoi(arg) : 0;
+	if(i >= 0 && i < ntags)
+		seltag[i] = True;
+	lt->arrange();
 }