Skip to content

Commit

Permalink
Make first_tab_idx window specific
Browse files Browse the repository at this point in the history
  • Loading branch information
tihirvon committed Apr 24, 2011
1 parent 964760a commit b3cb74b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
5 changes: 3 additions & 2 deletions screen.c
Expand Up @@ -107,15 +107,16 @@ static void print_tab_title(struct view *v, int idx)

void print_tabbar(void)
{
int idx, first_tab_idx = calculate_tabbar();
int idx;

buf_reset(window->x, window->w, 0);
buf_move_cursor(window->x, window->y);

calculate_tabbar(window);
for (idx = 0; idx < window->views.count; idx++) {
struct view *v = window->views.ptrs[idx];

if (idx < first_tab_idx)
if (idx < window->first_tab_idx)
continue;

if (obuf.x + v->tt_truncated_width > window->w)
Expand Down
59 changes: 28 additions & 31 deletions tabbar.c
Expand Up @@ -2,8 +2,6 @@
#include "window.h"
#include "uchar.h"

static int first_tab_idx;

static int filename_width(const char *filename)
{
int w = 0, i = 0;
Expand Down Expand Up @@ -32,60 +30,60 @@ static void update_tab_title_width(struct view *v, int tab_number)
v->tt_truncated_width = w;
}

static void update_first_tab_idx(void)
static void update_first_tab_idx(struct window *win)
{
int min_first_idx, max_first_idx, w;

w = 0;
for (max_first_idx = window->views.count; max_first_idx > 0; max_first_idx--) {
struct view *v = window->views.ptrs[max_first_idx - 1];
for (max_first_idx = win->views.count; max_first_idx > 0; max_first_idx--) {
struct view *v = win->views.ptrs[max_first_idx - 1];
w += v->tt_truncated_width;
if (w > window->w)
if (w > win->w)
break;
}

w = 0;
for (min_first_idx = window->views.count; min_first_idx > 0; min_first_idx--) {
struct view *v = window->views.ptrs[min_first_idx - 1];
for (min_first_idx = win->views.count; min_first_idx > 0; min_first_idx--) {
struct view *v = win->views.ptrs[min_first_idx - 1];
if (w || v == view)
w += v->tt_truncated_width;
if (w > window->w)
if (w > win->w)
break;
}

if (first_tab_idx < min_first_idx)
first_tab_idx = min_first_idx;
if (first_tab_idx > max_first_idx)
first_tab_idx = max_first_idx;
if (win->first_tab_idx < min_first_idx)
win->first_tab_idx = min_first_idx;
if (win->first_tab_idx > max_first_idx)
win->first_tab_idx = max_first_idx;
}

int calculate_tabbar(void)
void calculate_tabbar(struct window *win)
{
int extra, i, truncated_count, total_w = 0;

for (i = 0; i < window->views.count; i++) {
struct view *v = window->views.ptrs[i];
for (i = 0; i < win->views.count; i++) {
struct view *v = win->views.ptrs[i];

if (v == view) {
// make sure current tab is visible
if (first_tab_idx > i)
first_tab_idx = i;
if (win->first_tab_idx > i)
win->first_tab_idx = i;
}
update_tab_title_width(v, i + 1);
total_w += v->tt_width;
}

if (total_w <= window->w) {
if (total_w <= win->w) {
// all tabs fit without truncating
first_tab_idx = 0;
return first_tab_idx;
win->first_tab_idx = 0;
return;
}

// truncate all wide tabs
total_w = 0;
truncated_count = 0;
for (i = 0; i < window->views.count; i++) {
struct view *v = window->views.ptrs[i];
for (i = 0; i < win->views.count; i++) {
struct view *v = win->views.ptrs[i];
int truncated_w = 20;

if (v->tt_width > truncated_w) {
Expand All @@ -97,22 +95,22 @@ int calculate_tabbar(void)
}
}

if (total_w > window->w) {
if (total_w > win->w) {
// not all tabs fit even after truncating wide tabs
update_first_tab_idx();
return first_tab_idx;
update_first_tab_idx(win);
return;
}

// all tabs fit after truncating wide tabs
extra = window->w - total_w;
extra = win->w - total_w;

// divide extra space between truncated tabs
while (extra > 0) {
int extra_avg = extra / truncated_count;
int extra_mod = extra % truncated_count;

for (i = 0; i < window->views.count; i++) {
struct view *v = window->views.ptrs[i];
for (i = 0; i < win->views.count; i++) {
struct view *v = win->views.ptrs[i];
int add = v->tt_width - v->tt_truncated_width;
int avail;

Expand All @@ -137,6 +135,5 @@ int calculate_tabbar(void)
}
}

first_tab_idx = 0;
return first_tab_idx;
win->first_tab_idx = 0;
}
4 changes: 3 additions & 1 deletion tabbar.h
@@ -1,6 +1,8 @@
#ifndef TABBAR_H
#define TABBAR_H

int calculate_tabbar(void);
#include "window.h"

void calculate_tabbar(struct window *win);

#endif
2 changes: 2 additions & 0 deletions window.h
Expand Up @@ -23,6 +23,8 @@ struct window {
int first;
int last;
} line_numbers;

int first_tab_idx;
};

extern struct window *window;
Expand Down

0 comments on commit b3cb74b

Please sign in to comment.