summaryrefslogtreecommitdiffstats
path: root/window.c
diff options
context:
space:
mode:
Diffstat (limited to 'window.c')
-rw-r--r--window.c26
1 files changed, 14 insertions, 12 deletions
diff --git a/window.c b/window.c
index eccaa4e5..630533ee 100644
--- a/window.c
+++ b/window.c
@@ -122,16 +122,20 @@ winlink_find_by_index(struct winlinks *wwl, int idx)
}
int
-winlink_next_index(struct winlinks *wwl)
+winlink_next_index(struct winlinks *wwl, int idx)
{
- u_int i;
+ int i;
- for (i = 0; i < INT_MAX; i++) {
+ i = idx;
+ do {
if (winlink_find_by_index(wwl, i) == NULL)
return (i);
- }
-
- fatalx("no free indexes");
+ if (i == INT_MAX)
+ i = 0;
+ else
+ i++;
+ } while (i != idx);
+ return (-1);
}
u_int
@@ -152,14 +156,12 @@ winlink_add(struct winlinks *wwl, struct window *w, int idx)
{
struct winlink *wl;
- if (idx == -1)
- idx = winlink_next_index(wwl);
- else if (winlink_find_by_index(wwl, idx) != NULL)
+ if (idx < 0) {
+ if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
+ return (NULL);
+ } else if (winlink_find_by_index(wwl, idx) != NULL)
return (NULL);
- if (idx < 0)
- fatalx("bad index");
-
wl = xcalloc(1, sizeof *wl);
wl->idx = idx;
wl->window = w;