JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
applied sanders patch to remove unnecessary commit()
[dwm.git] / view.c
diff --git a/view.c b/view.c
index 0108a23..aa0a23a 100644 (file)
--- a/view.c
+++ b/view.c
@@ -3,12 +3,58 @@
  * See LICENSE file for license details.
  */
 #include "dwm.h"
+#include <stdio.h>
+
+/* static */
+
+static Client *
+minclient()
+{
+       Client *c, *min;
+
+       for(min = c = clients; c; c = c->next)
+               if(c->weight < min->weight)
+                       min = c;
+       return min;
+}
+
+
+static void
+reorder()
+{
+       Client *c, *newclients, *tail;
+
+       newclients = tail = NULL;
+       while((c = minclient())) {
+               detach(c);
+               if(tail) {
+                       c->prev = tail;
+                       tail->next = c;
+                       tail = c;
+               }
+               else
+                       tail = newclients = c;
+       }
+       clients = newclients;
+}
 
 /* extern */
 
 void (*arrange)(Arg *) = DEFMODE;
 
 void
+detach(Client *c)
+{
+       if(c->prev)
+               c->prev->next = c->next;
+       if(c->next)
+               c->next->prev = c->prev;
+       if(c == clients)
+               clients = c->next;
+       c->next = c->prev = NULL;
+}
+
+void
 dofloat(Arg *arg)
 {
        Client *c;
@@ -211,6 +257,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);
 }
 
@@ -222,32 +269,25 @@ view(Arg *arg)
        for(i = 0; i < ntags; i++)
                seltag[i] = False;
        seltag[arg->i] = True;
+       reorder();
        arrange(NULL);
 }
 
 void
 zoom(Arg *arg)
 {
-       Client *c;
+       Client *c = sel;
 
-       if(!sel || (arrange != dotile) || sel->isfloat || sel->ismax)
+       if(!c || (arrange != dotile) || c->isfloat || c->ismax)
                return;
 
-       if(sel == getnext(clients))  {
-               if((c = getnext(sel->next)))
-                       sel = c;
-               else
+       if(c == getnext(clients))
+               if(!(c = getnext(c->next)))
                        return;
-       }
-
-       /* pop */
-       sel->prev->next = sel->next;
-       if(sel->next)
-               sel->next->prev = sel->prev;
-       sel->prev = NULL;
-       clients->prev = sel;
-       sel->next = clients;
-       clients = sel;
-       focus(sel);
+       detach(c);
+       c->next = clients;
+       clients->prev = c;
+       clients = c;
+       focus(c);
        arrange(NULL);
 }