JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
applied Jeroen's {clean,spell}.diff patches, thanks Jeroen!
[dwm.git] / tag.c
1 /* See LICENSE file for copyright and license details. */
2 #include "dwm.h"
3 #include <regex.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <X11/Xutil.h>
7
8 /* static */
9
10 typedef struct {
11         const char *prop;
12         const char *tags;
13         Bool isfloating;
14 } Rule;
15
16 typedef struct {
17         regex_t *propregex;
18         regex_t *tagregex;
19 } Regs;
20
21 TAGS
22 RULES
23
24 static Regs *regs = NULL;
25 static unsigned int nrules = 0;
26
27 /* extern */
28
29 void
30 compileregs(void) {
31         unsigned int i;
32         regex_t *reg;
33
34         if(regs)
35                 return;
36         nrules = sizeof rule / sizeof rule[0];
37         regs = emallocz(nrules * sizeof(Regs));
38         for(i = 0; i < nrules; i++) {
39                 if(rule[i].prop) {
40                         reg = emallocz(sizeof(regex_t));
41                         if(regcomp(reg, rule[i].prop, REG_EXTENDED))
42                                 free(reg);
43                         else
44                                 regs[i].propregex = reg;
45                 }
46                 if(rule[i].tags) {
47                         reg = emallocz(sizeof(regex_t));
48                         if(regcomp(reg, rule[i].tags, REG_EXTENDED))
49                                 free(reg);
50                         else
51                                 regs[i].tagregex = reg;
52                 }
53         }
54 }
55
56 Bool
57 isvisible(Client *c) {
58         unsigned int i;
59
60         for(i = 0; i < ntags; i++)
61                 if(c->tags[i] && seltag[i])
62                         return True;
63         return False;
64 }
65
66 void
67 settags(Client *c, Client *trans) {
68         char prop[512];
69         unsigned int i, j;
70         regmatch_t tmp;
71         Bool matched = trans != NULL;
72         XClassHint ch = { 0 };
73
74         if(matched)
75                 for(i = 0; i < ntags; i++)
76                         c->tags[i] = trans->tags[i];
77         else {
78                 XGetClassHint(dpy, c->win, &ch);
79                 snprintf(prop, sizeof prop, "%s:%s:%s",
80                                 ch.res_class ? ch.res_class : "",
81                                 ch.res_name ? ch.res_name : "", c->name);
82                 for(i = 0; i < nrules; i++)
83                         if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
84                                 c->isfloating = rule[i].isfloating;
85                                 for(j = 0; regs[i].tagregex && j < ntags; j++) {
86                                         if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
87                                                 matched = True;
88                                                 c->tags[j] = True;
89                                         }
90                                 }
91                         }
92                 if(ch.res_class)
93                         XFree(ch.res_class);
94                 if(ch.res_name)
95                         XFree(ch.res_name);
96         }
97         if(!matched)
98                 for(i = 0; i < ntags; i++)
99                         c->tags[i] = seltag[i];
100 }
101
102 void 
103 shiftview(const char *arg) {
104         int i, j;
105
106         for(i = 0; !seltag[i]; i++);
107         for(j = i + 1; j < ntags && !seltag[j]; j++);
108         if(j < ntags)
109                 return; /* more than one tag selected */
110         seltag[i] = False;
111         i += arg ? atoi(arg) : 0;
112         if(i < 0)
113                 i = ntags - 1;
114         else if(i >= ntags)
115                 i = 0;
116         seltag[i] = True;
117         lt->arrange();
118 }
119
120 void
121 tag(const char *arg) {
122         int i;
123
124         if(!sel)
125                 return;
126         for(i = 0; i < ntags; i++)
127                 sel->tags[i] = arg == NULL;
128         i = arg ? atoi(arg) : 0;
129         if(i >= 0 && i < ntags)
130                 sel->tags[i] = True;
131         lt->arrange();
132 }
133
134 void
135 toggletag(const char *arg) {
136         int i, j;
137
138         if(!sel)
139                 return;
140         i = arg ? atoi(arg) : 0;
141         sel->tags[i] = !sel->tags[i];
142         for(j = 0; j < ntags && !sel->tags[j]; j++);
143         if(j == ntags)
144                 sel->tags[i] = True;
145         lt->arrange();
146 }
147
148 void
149 toggleview(const char *arg) {
150         int i, j;
151
152         i = arg ? atoi(arg) : 0;
153         seltag[i] = !seltag[i];
154         for(j = 0; j < ntags && !seltag[j]; j++);
155         if(j == ntags)
156                 seltag[i] = True; /* cannot toggle last view */
157         lt->arrange();
158 }
159
160 void
161 view(const char *arg) {
162         int i;
163
164         for(i = 0; i < ntags; i++)
165                 seltag[i] = arg == NULL;
166         i = arg ? atoi(arg) : 0;
167         if(i >= 0 && i < ntags)
168                 seltag[i] = True;
169         lt->arrange();
170 }