JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
improved selection policy
[dwm.git] / view.c
diff --git a/view.c b/view.c
index 9ba8e0b..5de34c0 100644 (file)
--- a/view.c
+++ b/view.c
@@ -3,38 +3,45 @@
  * See LICENSE file for license details.
  */
 #include "dwm.h"
+#include <stdio.h>
 
-/* extern */
+/* static */
 
-void (*arrange)(Arg *) = DEFMODE;
+static Client *
+minclient()
+{
+       Client *c, *min;
 
-void
-attach(Client *c)
+       for(min = c = clients; c; c = c->next)
+               if(c->weight < min->weight)
+                       min = c;
+       return min;
+}
+
+
+static void
+reorder()
 {
-       Client *first = getnext(clients);
+       Client *c, *newclients, *tail;
 
-       if(!first) {
-               if(clients) {
-                       for(first = clients; first->next; first = first->next);
-                       first->next = c;
-                       c->prev = first;
+       newclients = tail = NULL;
+       while((c = minclient())) {
+               detach(c);
+               if(tail) {
+                       c->prev = tail;
+                       tail->next = c;
+                       tail = c;
                }
                else
-                       clients = c;
-       }
-       else if(first == clients) {
-               c->next = clients;
-               clients->prev = c;
-               clients = c;
-       }
-       else {
-               first->prev->next = c;
-               c->prev = first->prev;
-               first->prev = c;
-               c->next = first;
+                       tail = newclients = c;
        }
+       clients = newclients;
 }
 
+/* extern */
+
+void (*arrange)(Arg *) = DEFMODE;
+
 void
 detach(Client *c)
 {
@@ -60,9 +67,7 @@ dofloat(Arg *arg)
                else
                        ban(c);
        }
-       if(!sel || !isvisible(sel))
-               sel = getnext(clients);
-       if(sel)
+       if((sel = getnext(clients)))
                focus(sel);
        else
                XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
@@ -125,9 +130,7 @@ dotile(Arg *arg)
                else
                        ban(c);
        }
-       if(!sel || !isvisible(sel))
-               sel = getnext(clients);
-       if(sel)
+       if((sel = getnext(clients)))
                focus(sel);
        else
                XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
@@ -250,6 +253,7 @@ toggleview(Arg *arg)
        for(i = 0; i < ntags && !seltag[i]; i++);
        if(i == ntags)
                seltag[arg->i] = True; /* cannot toggle last view */
+       reorder();
        arrange(NULL);
 }
 
@@ -257,10 +261,12 @@ void
 view(Arg *arg)
 {
        unsigned int i;
+       Client *c;
 
        for(i = 0; i < ntags; i++)
                seltag[i] = False;
        seltag[arg->i] = True;
+       reorder();
        arrange(NULL);
 }
 
@@ -276,7 +282,9 @@ zoom(Arg *arg)
                if(!(c = getnext(c->next)))
                        return;
        detach(c);
-       attach(c);
+       c->next = clients;
+       clients->prev = c;
+       clients = c;
        focus(c);
        arrange(NULL);
 }