- if (!next_sel) { // if no visible window prev, find first visible
- for(i = c->mon->stack; i && !ISVISIBLE(i); i = i->snext)
+ // find last visible window before c
+ // WARNING if you detach() before detachstack() this will select last visible window
+ for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
+ next_sel = i;
+ // failing that, find first visible window
+ if (!next_sel)
+ for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))