+void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
+{
+ if (n == NULL)
+ return;
+
+ PUTS("nearest leaf");
+
+ node_t *x = NULL;
+
+ for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
+ if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
+ || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
+ || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
+ if ((dir == NEAREST_OLDER
+ && (f->client->uid < n->client->uid)
+ && (x == NULL || f->client->uid > x->client->uid))
+ || (dir == NEAREST_NEWER
+ && (f->client->uid > n->client->uid)
+ && (x == NULL || f->client->uid < x->client->uid)))
+ x = f;
+
+ focus_node(m, d, x, true);
+}
+
+void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
+ if (d == NULL || d->root == NULL || is_leaf(d->root))
+ return;
+ node_t *par = d->focus->parent;
+ bool focus_first_child = is_first_child(d->focus);
+ if (dir == CIRCULATE_FORWARD)
+ for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
+ swap_nodes(f, s);
+ else
+ for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
+ swap_nodes(f, s);
+ if (focus_first_child)
+ focus_node(m, d, par->first_child, true);
+ else
+ focus_node(m, d, par->second_child, true);
+}
+