-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabs.c
153 lines (113 loc) · 2.27 KB
/
tabs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include "pos.h"
#include "region.h"
#include "list.h"
#include "buffer.h"
#include "window.h"
#include "windows.h"
#include "main.h"
#include "tab.h"
#include "tabs.h"
#include "ui.h"
#include "tabs.h"
static tab *tab_sel, *tab_first;
tab *tabs_cur()
{
return tab_sel;
}
void tabs_set_cur(tab *new)
{
tab_sel = new;
}
tab *tabs_first(void)
{
return tab_first;
}
void tabs_set_first(tab *t)
{
tab_first = t;
}
bool tabs_single(void)
{
return tabs_count() == 1;
}
unsigned tabs_count(void)
{
unsigned n = 0;
for(tab *t = tabs_first(); t; t = t->next, n++);
return n;
}
static void win_add_splitright(window *cur, window *new)
{
window_add_neighbour(cur, neighbour_right, new);
}
static void win_add_splittop(window *cur, window *new)
{
window_add_neighbour(cur, neighbour_down, new);
}
static void load_argv(void onload(window *cur, window *new))
{
window *prev_win = windows_cur();
char *next;
while((next = args_next_fname(true))){
buffer_t *buf;
const char *err;
buffer_new_fname(&buf, next, &err);
window *w = window_new(buf);
buffer_release(buf);
onload(prev_win, w);
prev_win = w;
}
}
void tabs_init(enum init_args init_args, unsigned off)
{
char *next = args_next_fname(true);
if(next){
const char *err = NULL;
buffer_t *buf;
if(!strcmp(next, "-")){
buf = buffer_new_file_nofind(stdin);
buf->modified = true; /* editing a stream */
/* no filename */
if(ferror(stdin))
err = strerror(errno);
freopen("/dev/tty", "r", stdin);
/* if we can't open it we'll exit soon anyway */
}else{
buffer_new_fname(&buf, next, &err);
}
if(err)
ui_err("\"%s\": %s", next, err);
tab *tab = tab_new(window_new(buf));
buffer_release(buf);
tabs_set_cur(tab);
switch(init_args){
case INIT_NONE:
break;
case INIT_VALL:
load_argv(win_add_splitright);
break;
case INIT_HALL:
load_argv(win_add_splittop);
break;
}
}else{
buffer_t *empty = buffer_new();
window *win = window_new(empty);
buffer_release(empty);
tab *tab = tab_new(win);
tabs_set_cur(tab);
}
tabs_set_first(tabs_cur());
windows_cur()->ui_pos->y = off;
}
void tabs_term(void)
{
if(tabs_cur()){
tab_free(tabs_cur());
tabs_set_cur(NULL);
}
}