+static inline int is_nearer(int r1, int r2)
+{
+ int a = r2 < active_win->refnum;
+ int b = r1 < r2;
+
+ if (r1 > active_win->refnum)
+ return a || b;
+ else
+ return a && b;
+}
+
+static WINDOW_REC *window_find_item_cycle(SERVER_REC *server, const char *name)
+{
+ WINDOW_REC *rec, *win;
+ GSList *tmp;
+
+ win = NULL;
+
+ tmp = g_slist_find(windows, active_win);
+ tmp = tmp->next;
+ for (;; tmp = tmp->next) {
+ if (tmp == NULL)
+ tmp = windows;
+
+ if (tmp->data == active_win)
+ break;
+
+ rec = tmp->data;
+
+ if (window_item_find_window(rec, server, name) != NULL &&
+ (win == NULL || is_nearer(rec->refnum, win->refnum))) {
+ win = rec;
+ if (server != NULL) break;
+ }
+ }
+
+ return win;
+}
+