From 39c157389dbaf5270508c4f0cd3cb679a71d22ed Mon Sep 17 00:00:00 2001 From: Kalle Olavi Niemitalo Date: Sun, 14 May 2006 22:17:26 +0300 Subject: [PATCH] Don't let move_current_tab move the tab beyond the main menu. src/terminal/tab.c (get_tab_by_number): Assert that the returned struct window * actually points to a struct window. src/terminal/tab.c (move_current_tab): Keep tabs contiguous in the stack of windows. Obey "ui.tabs.wraparound". src/terminal/terminal.h (struct terminal): Documented that tabs can move in the stack. src/terminal/window.c (assert_window_stacking): Check that the main menu isn't between tabs. --- src/terminal/tab.c | 31 +++++++++++++++++++++---------- src/terminal/terminal.h | 6 +++--- src/terminal/window.c | 18 ++++++++++++++---- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/terminal/tab.c b/src/terminal/tab.c index e914b5d25..709bd5dbd 100644 --- a/src/terminal/tab.c +++ b/src/terminal/tab.c @@ -103,6 +103,12 @@ get_tab_by_number(struct terminal *term, int num) num--; } + /* Ensure that the return value actually points to a struct + * window. */ + assertm((struct list_head *) win != &term->windows, + "tab number out of range"); + if_assert_failed return term->windows.next; + return win; } @@ -301,22 +307,27 @@ move_current_tab(struct session *ses, int direction) new_pos = term->current_tab + direction; - while (new_pos < 1 || new_pos > tabs) - new_pos += new_pos < 1 ? tabs : -tabs; - - assert(0 < new_pos && new_pos <= tabs); + if (get_opt_bool("ui.tabs.wraparound")) { + while (new_pos < 0 || new_pos >= tabs) + new_pos += new_pos < 0 ? tabs : -tabs; + } else { + if (new_pos < 0) + new_pos = 0; + else if (new_pos >= tabs) + new_pos = tabs - 1; + } + assert(0 <= new_pos && new_pos < tabs); + /* This protects against tabs==1 and optimizes an unusual case. */ if (new_pos == term->current_tab) return; - tab = get_tab_by_number(term, new_pos); - del_from_list(current_tab); - - if (new_pos < term->current_tab) { - add_at_pos(tab, current_tab); + if (new_pos == 0) { + tab = get_tab_by_number(term, 0); } else { - add_to_list_end(*tab, current_tab); + tab = get_tab_by_number(term, new_pos-1)->prev; } + add_at_pos(tab, current_tab); switch_to_tab(term, new_pos, tabs); } diff --git a/src/terminal/terminal.h b/src/terminal/terminal.h index 32d148f43..d6a46d3dc 100644 --- a/src/terminal/terminal.h +++ b/src/terminal/terminal.h @@ -73,9 +73,9 @@ struct terminal { * current window; and .prev is the bottom, covered by others. * - Dialogs or active menus are at the top. * - Next come all tabs (window.type == WINDOW_TAB). The tab - * created last is at the top, and the tab created first is - * at the bottom; but current_tab controls which tab is - * actually displayed. + * listed leftmost in the tab bar is at the top, and the tab + * listed rightmost is at the bottom; but current_tab controls + * which tab is actually displayed. * - If the main menu is inactive, then it is at the very bottom, * hidden under the tabs. * Call assert_window_stacking to verify this. diff --git a/src/terminal/window.c b/src/terminal/window.c index 1099aabb5..3bad556d9 100644 --- a/src/terminal/window.c +++ b/src/terminal/window.c @@ -178,7 +178,7 @@ add_empty_window(struct terminal *term, void (*fn)(void *), void *data) void assert_window_stacking(struct terminal *term) { - int prev_type = WINDOW_NORMAL; + enum { WANT_ANY, WANT_TAB, WANT_NONE } want = WANT_ANY; const struct window *win; const struct window *main_menu_win; @@ -186,10 +186,20 @@ assert_window_stacking(struct terminal *term) main_menu_win = term->main_menu ? term->main_menu->win : NULL; foreach (win, term->windows) { - if (win != main_menu_win) { - if (prev_type == WINDOW_TAB) + switch (want) { + case WANT_ANY: + if (win->type == WINDOW_TAB) + want = WANT_TAB; + break; + case WANT_TAB: + if (win == main_menu_win) + want = WANT_NONE; + else assert(win->type == WINDOW_TAB); - prev_type = win->type; + break; + case WANT_NONE: + assert(0); + break; } } }