- GList *it;
-
- for (it = client_list; it; it = g_list_next(it)) {
- ObClient *c = it->data;
- if (c->desktop != DESKTOP_ALL && c->desktop > screen_desktop)
- client_set_desktop(c, c->desktop-1, FALSE, TRUE);
+ GList *it, *stacking_copy;
+
+ /* make a copy of the list cuz we're changing it */
+ stacking_copy = g_list_copy(stacking_list);
+ for (it = g_list_last(stacking_copy); it; it = g_list_previous(it)) {
+ if (WINDOW_IS_CLIENT(it->data)) {
+ ObClient *c = it->data;
+ if (c->desktop != DESKTOP_ALL && c->desktop > screen_desktop)
+ client_set_desktop(c, c->desktop - 1, TRUE, TRUE);
+ /* raise all the windows that are on the current desktop which
+ is being merged */
+ else if (c->desktop == DESKTOP_ALL ||
+ c->desktop == screen_desktop)
+ stacking_raise(CLIENT_AS_WINDOW(c));
+ }