void
attach(Client *c) {
- c->next = c->mon->clients;
- c->mon->clients = c;
+ if (c->mon->sel) {
+ c->next = c->mon->sel->next;
+ c->mon->sel->next = c;
+ } else {
+ c->next = c->mon->clients;
+ c->mon->clients = c;
+ }
}
void
attachstack(Client *c) {
- c->snext = c->mon->stack;
- c->mon->stack = c;
+ if (c->mon->sel) {
+ c->snext = c->mon->sel->snext;
+ c->mon->sel->snext = c;
+ } else {
+ c->snext = c->mon->stack;
+ c->mon->stack = c;
+ }
}
void
void
detachstack(Client *c) {
- Client **tc, *t;
-
- for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
- *tc = c->snext;
-
+ Client *prev = NULL, *next_sel = NULL, *i;
+ for(i = c->mon->stack; i && i != c; i = i->snext) {
+ prev = i;
+ if(ISVISIBLE(i))
+ next_sel = i;
+ }
if(c == c->mon->sel) {
- for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
- c->mon->sel = t;
+ if (!next_sel) { // if no visible window prev, find first visible
+ for(i = c->mon->stack; i && !ISVISIBLE(i); i = i->snext)
+ next_sel = i;
+ }
+ c->mon->sel = next_sel;
+ }
+ if (prev) {
+ prev->snext = c->snext;
+ } else {
+ c->mon->stack = c->snext;
}
}
c->mon->sel = c;
arrange(c->mon);
XMapWindow(dpy, c->win);
- focus(NULL);
+ focus(c);
}
void
XUngrabServer(dpy);
}
free(c);
- focus(NULL);
+ focus(selmon ? selmon->sel : NULL);
updateclientlist();
arrange(m);
}