+ if (n == NULL || !is_tiled(n->client))
+ return NULL;
+
+ node_t *target = find_fence(n, dir);
+ if (target == NULL)
+ return NULL;
+ if (dir == DIR_UP || dir == DIR_LEFT)
+ target = target->first_child;
+ else if (dir == DIR_DOWN || dir == DIR_RIGHT)
+ target = target->second_child;
+
+ node_t *nearest = NULL;
+ int min_rank = INT_MAX;
+
+ for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
+ if (!is_tiled(a->client) || !is_adjacent(a, target) || a == n)
+ continue;
+ int rank = history_rank(f, a);
+ if (rank >= 0 && rank < min_rank) {
+ nearest = a;
+ min_rank = rank;
+ }