I improved the code a little bit and made it more efficient.  I was using
two loops to find the pointers before the selected client and the swapped
client when I only really needed one.  I'll probably put this up on the dwm
patch website later this week.

-Niki

On Tue, Oct 7, 2008 at 11:26 PM, Monsieur Pinocchio <[EMAIL PROTECTED]>wrote:

> On Tue, Oct 7, 2008 at 10:36 PM, Niki Yoshiuchi <[EMAIL PROTECTED]> wrote:
>
>> Hello!  This is my first message to this mailing list so I thought I'd
>> start by contributing a patch.  This patch allows you to move clients around
>> to reposition them in a tiled layout.  The behavior is meant to emulate
>> Xmonad's mod-shift-k and mod-shift-j.  m-s-j swaps a client with the next
>> visible, tiled client in the clients list.  m-s-k swaps the client with the
>> previous.
>>
>> I hope some of you find this useful and I certainly hope to contribute
>> more in the future.
>>
>> -Niki Yoshiuchi
>>
>
> Thanks a lot for this patch... I really liked it!
>
> --
> Pinocchio
>
diff -ruN dwm/config.def.h movestack/config.def.h
--- dwm/config.def.h	2008-10-04 15:57:46.000000000 -0400
+++ movestack/config.def.h	2008-10-05 17:13:15.000000000 -0400
@@ -50,6 +50,7 @@
 static const char *dmenucmd[] = { "dmenu_run", "-fn", font, "-nb", normbgcolor, "-nf", normfgcolor, "-sb", selbgcolor, "-sf", selfgcolor, NULL };
 static const char *termcmd[]  = { "uxterm", NULL };
 
+#include "movestack.c"
 static Key keys[] = {
 	/* modifier                     key        function        argument */
 	{ MODKEY,                       XK_p,      spawn,          {.v = dmenucmd } },
@@ -59,6 +60,8 @@
 	{ MODKEY,                       XK_k,      focusstack,     {.i = -1 } },
 	{ MODKEY,                       XK_h,      setmfact,       {.f = -0.05} },
 	{ MODKEY,                       XK_l,      setmfact,       {.f = +0.05} },
+	{ MODKEY|ShiftMask,             XK_j,      movestack,      {.i = +1 } },
+	{ MODKEY|ShiftMask,             XK_k,      movestack,      {.i = -1 } },
 	{ MODKEY,                       XK_Return, zoom,           {0} },
 	{ MODKEY,                       XK_Tab,    view,           {0} },
 	{ MODKEY|ShiftMask,             XK_c,      killclient,     {0} },
diff -ruN dwm/movestack.c movestack/movestack.c
--- dwm/movestack.c	1969-12-31 19:00:00.000000000 -0500
+++ movestack/movestack.c	2008-10-10 00:01:52.000000000 -0400
@@ -0,0 +1,49 @@
+void
+movestack(const Arg *arg) {
+	Client *c = NULL, *p = NULL, *pc = NULL, *i;
+
+	if(arg->i > 0) {
+		/* find the client after sel */
+		for(c = sel->next; c && (!ISVISIBLE(c) || c->isfloating); c = c->next);
+		if(!c)
+			for(c = clients; c && (!ISVISIBLE(c) || c->isfloating); c = c->next);
+
+	}
+	else {
+		/* find the client before sel */
+		for(i = clients; i != sel; i = i->next)
+			if(ISVISIBLE(i) && !i->isfloating)
+				c = i;
+		if(!c)
+			for(; i; i = i->next)
+				if(ISVISIBLE(i) && !i->isfloating)
+					c = i;
+	}
+	/* find the client before sel and c */
+	for(i = clients; i && (!p || !pc); i = i->next) {
+		if(i->next == sel)
+			p = i;
+		if(i->next == c)
+			pc = i;
+	}
+
+	/* swap c and sel clients in the clients list */
+	if(c && c != sel) {
+		Client *temp = sel->next==c?sel:sel->next;
+		sel->next = c->next==sel?c:c->next;
+		c->next = temp;
+
+		if(p && p != c)
+			p->next = c;
+		if(pc && pc != sel)
+			pc->next = sel;
+
+		if(sel == clients)
+			clients = c;
+		else if(c == clients)
+			clients = sel;
+
+		arrange();
+	}
+}
+

Reply via email to