-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
112 lines (98 loc) · 2.57 KB
/
main.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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <termbox.h>
#include <unistd.h>
#include "editor.h"
#include "tags.h"
#include "terminal.h"
#include "util.h"
#include "window.h"
int main(int argc, char *argv[]) {
char *line = NULL;
char *tag = NULL;
enum window_split_type split_type = WINDOW_LEAF;
bool pathargs[argc];
memset(pathargs, 0, sizeof(pathargs));
char *initial_path = NULL;
char *rc = NULL;
char default_rc[255];
for (int i = 1; i < argc; ++i) {
char *arg = argv[i];
if (!strcmp(arg, "-u")) {
if (i == argc - 1) {
fprintf(stderr, "argument missing after -u\n");
return 1;
}
rc = argv[++i];
} else if (!strcmp(arg, "-t")) {
if (i == argc - 1) {
fprintf(stderr, "argument missing after -t\n");
return 1;
}
tag = argv[++i];
} else if (!strcmp(arg, "-o")) {
split_type = WINDOW_SPLIT_HORIZONTAL;
} else if (!strcmp(arg, "-O")) {
split_type = WINDOW_SPLIT_VERTICAL;
} else if (*arg == '+') {
line = arg + 1;
} else {
if (!initial_path) {
initial_path = arg;
} else {
pathargs[i] = true;
}
}
}
terminal_init();
struct editor *editor = editor_create_and_open(
(size_t) tb_width(), (size_t) tb_height(), initial_path);
if (!rc) {
snprintf(default_rc, sizeof(default_rc), "%s/.badavimrc", homedir());
if (!access(default_rc, F_OK)) {
rc = default_rc;
}
}
if (rc && strcmp(rc, "NONE") != 0) {
editor_source(editor, rc);
}
if (tag) {
editor_jump_to_tag(editor, tag);
} else {
if (split_type != WINDOW_LEAF) {
enum window_split_direction direction;
if (split_type == WINDOW_SPLIT_HORIZONTAL) {
direction = WINDOW_SPLIT_BELOW;
} else {
assert(split_type == WINDOW_SPLIT_VERTICAL);
direction = WINDOW_SPLIT_RIGHT;
}
for (int i = 1; i < argc; ++i) {
if (pathargs[i]) {
editor->window = window_split(editor->window, direction);
editor_open(editor, argv[i]);
}
}
editor->window = window_first_leaf(window_root(editor->window));
window_equalize(editor->window, split_type);
}
if (line) {
if (!*line) {
editor_jump_to_end(editor);
} else {
int linenum;
if (strtoi(line, &linenum)) {
editor_jump_to_line(editor, linenum - 1);
}
}
}
}
editor_draw(editor);
struct tb_event ev;
while (editor_waitkey(editor, &ev)) {
editor_handle_key_press(editor, &ev);
editor_draw(editor);
}
return 0;
}