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
|
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <locale.h>
#include <curses.h>
#include "page.cpp"
#include "point.cpp"
#include "buffer.cpp"
#define NORMAL_MODE 0
#define INSERT_MODE 1
#define pos(x, y) (x) + (y) * window_width
int main(int argc, char *argv[]) {
initscr();
cbreak();
noecho();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
if (argc > 1) {
buffer.read(argv[1]);
}
int window_height, window_width;
getmaxyx(stdscr, window_height, window_width);
int *view = new int[window_width * window_height];
for (int i = 0; i < window_width * window_height; view[i++] = 0);
int mode = NORMAL_MODE;
int quit = 0;
while (!quit) {
clear();
Point window_end(window_start);
int x = -1, y = -1;
for (int i = 0; i < window_height; i++) {
for (int j = 0; j < window_width; j++) {
if (window_end == cursor) {
x = j;
y = i;
}
view[pos(j, i)] = window_end.element();
if (window_end.element() == '\n') {
for (int k = j + 1; k < window_width; k++) {
view[pos(k, i)] = 0;
}
j = window_width;
} else if (window_end.element() == '\t') {
for (int k = j + 1; k < j + 8; k++) {
view[pos(k, i)] = 0;
}
j = j + 7;
}
window_end++;
}
}
for (int i = 0; i < window_width * window_height; i++) {
printw("%lc", view[i]);
}
if (x != -1 && y != -1) {
move(y, x);
}
int input = getch();
if (byte_type(input) == 1) {
if (mode == NORMAL_MODE) {
switch (input) {
case '':
quit = 1;
break;
case 'i':
mode = INSERT_MODE;
break;
case 'j':
for (int i = pos(x, y); i < pos(x + 1, y + 1); i++) {
if (view[i]) cursor++;
}
cursor--;
if (y + 1 >= window_height) {
window_start.seek('\n', window_width - 1);
window_start++;
}
break;
case 'k':
for (int i = pos(x, y); i > pos(x, y - 1); i--) {
if (view[i]) cursor--;
}
if (y - 1 <= 0) {
window_start--;
window_start.rseek('\n', window_width - 1);
}
break;
case 'h':
cursor--;
break;
case 'l':
cursor++;
break;
}
} else {
switch (input) {
case '':
mode = NORMAL_MODE;
break;
case KEY_BACKSPACE:
cursor.pop();
break;
default:
cursor.push(input);
}
}
} else {
cursor.push(input);
for (int i = 0; i < byte_type(input) - 1; i++) {
cursor.push(getch());
}
}
}
buffer.write(buffer.name);
endwin();
return 0;
}
|