about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorarg@10ksloc.org <unknown>2006-08-04 14:40:32 +0200
committerarg@10ksloc.org <unknown>2006-08-04 14:40:32 +0200
commite21d93b7bd5d34f31bc09a576b7d449df5b68c07 (patch)
treefb5a123126b80d8cb9480aaec8c320fecfc2fec4
parentf504aea13289c7fd25d499d2582558d4e311bfbf (diff)
downloaddwm-e21d93b7bd5d34f31bc09a576b7d449df5b68c07.tar.gz
switched to regexp matching for Rules
-rw-r--r--config.arg.h11
-rw-r--r--config.default.h7
-rw-r--r--config.mk8
-rw-r--r--dwm.h1
-rw-r--r--main.c1
-rw-r--r--tag.c68
6 files changed, 65 insertions, 31 deletions
diff --git a/config.arg.h b/config.arg.h
index 4297376..d391f9e 100644
--- a/config.arg.h
+++ b/config.arg.h
@@ -52,11 +52,10 @@ static Key key[] = { \
 };
 
 #define RULES \
-	const unsigned int two[] = { 2 }; \
 static Rule rule[] = { \
-	/* class:instance	tags		isfloat */ \
-	{ "Firefox.*",		two,		False }, \
-	{ "Gimp.*",		NULL,		True}, \
-	{ "MPlayer.*",		NULL,		True}, \
-	{ "Acroread.*",		NULL,		True}, \
+	/* class:instance regex		tags regex	isfloat */ \
+	{ "Firefox.*",			"net",		False }, \
+	{ "Gimp.*",			NULL,		True}, \
+	{ "MPlayer.*",			NULL,		True}, \
+	{ "Acroread.*",			NULL,		True}, \
 };
diff --git a/config.default.h b/config.default.h
index f3fd7b7..3847ff8 100644
--- a/config.default.h
+++ b/config.default.h
@@ -47,9 +47,8 @@ static Key key[] = { \
 };
 
 #define RULES \
-	const unsigned int two[] = { 2 }; \
 static Rule rule[] = { \
-	/* class:instance	tags		isfloat */ \
-	{ "Firefox.*",		two,	False }, \
-	{ "Gimp.*",		NULL,		True}, \
+	/* class:instance regex		tags regex	isfloat */ \
+	{ "Firefox.*",			"2",		False }, \
+	{ "Gimp.*",			NULL,		True}, \
 };
diff --git a/config.mk b/config.mk
index 4657aff..92e244e 100644
--- a/config.mk
+++ b/config.mk
@@ -15,10 +15,10 @@ INCS = -I/usr/lib -I${X11INC}
 LIBS = -L/usr/lib -lc -L${X11LIB} -lX11
 
 # flags
-CFLAGS = -O3 ${INCS} -DVERSION=\"${VERSION}\"
-LDFLAGS = ${LIBS}
-#CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\"
-#LDFLAGS = -g ${LIBS}
+#CFLAGS = -O3 ${INCS} -DVERSION=\"${VERSION}\"
+#LDFLAGS = ${LIBS}
+CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\"
+LDFLAGS = -g ${LIBS}
 
 # compiler
 CC = cc
diff --git a/dwm.h b/dwm.h
index 939b546..57d628a 100644
--- a/dwm.h
+++ b/dwm.h
@@ -121,6 +121,7 @@ extern int xerror(Display *dsply, XErrorEvent *ee);
 extern void appendtag(Arg *arg);
 extern void dofloat(Arg *arg);
 extern void dotile(Arg *arg);
+extern void initrregs();
 extern Client *getnext(Client *c);
 extern Client *getprev(Client *c);
 extern void replacetag(Arg *arg);
diff --git a/main.c b/main.c
index 9ea09bf..633966b 100644
--- a/main.c
+++ b/main.c
@@ -210,6 +210,7 @@ main(int argc, char *argv[])
 	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
 
 	grabkeys();
+	initrregs();
 
 	for(ntags = 0; tags[ntags]; ntags++);
 
diff --git a/tag.c b/tag.c
index b20e71e..015a06a 100644
--- a/tag.c
+++ b/tag.c
@@ -5,21 +5,31 @@
 #include "dwm.h"
 #include <regex.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <X11/Xutil.h>
 
-/* static */
 
 typedef struct {
-	const char *pattern;
-	const unsigned int *tags;
+	const char *clpattern;
+	const char *tpattern;
 	Bool isfloat;
 } Rule;
 
+typedef struct {
+	regex_t *clregex;
+	regex_t *tregex;
+} RReg;
+
+/* static */
+
 TAGS
 RULES
 
+static RReg *rreg = NULL;
+static unsigned int len = 0;
+
 void (*arrange)(Arg *) = DEFMODE;
 
 /* extern */
@@ -138,6 +148,35 @@ getprev(Client *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
 replacetag(Arg *arg)
 {
 	int i;
@@ -154,9 +193,7 @@ void
 settags(Client *c)
 {
 	char classinst[256];
-	static unsigned int len = sizeof(rule) / sizeof(rule[0]);
-	unsigned int i, j, n;
-	regex_t regex;
+	unsigned int i, j;
 	regmatch_t tmp;
 	Bool matched = False;
 	XClassHint ch;
@@ -165,19 +202,16 @@ settags(Client *c)
 		snprintf(classinst, sizeof(classinst), "%s:%s",
 				ch.res_class ? ch.res_class : "",
 				ch.res_name ? ch.res_name : "");
-		for(i = 0; !matched && i < len; i++) {
-			if(!regcomp(&regex, rule[i].pattern, 0)) {
-				if(!regexec(&regex, classinst, 1, &tmp, 0)) {
-					n = rule[i].tags ?
-						sizeof(rule[i].tags) / sizeof(rule[i].tags[0]) : 0;
-					matched = n != 0;
-					for(j = 0; j < n; j++)
-						c->tags[rule[i].tags[j]] = True;
-					c->isfloat = rule[i].isfloat;
+		for(i = 0; !matched && i < len; i++)
+			if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
+				c->isfloat = rule[i].isfloat;
+				for(j = 0; rreg[i].tregex && j < ntags; j++) {
+					if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
+						matched = True;
+						c->tags[j] = True;
+					}
 				}
-				regfree(&regex);
 			}
-		}
 		if(ch.res_class)
 			XFree(ch.res_class);
 		if(ch.res_name)
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724