summaryrefslogtreecommitdiff
path: root/jet.c
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-05-09 22:02:52 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-05-09 22:02:52 -0300
commitd37027bbd7ac13fdd0f1e2f01e1ec4b75b6c9588 (patch)
tree5df5808fba713265350513b4e06686ab69c79819 /jet.c
parent137f5f6bbe935e71ca66bb9aafd0114aaab654f0 (diff)
downloadjet-d37027bbd7ac13fdd0f1e2f01e1ec4b75b6c9588.tar.gz
jet-d37027bbd7ac13fdd0f1e2f01e1ec4b75b6c9588.zip
Extract buffer structure
Diffstat (limited to 'jet.c')
-rw-r--r--jet.c41
1 files changed, 27 insertions, 14 deletions
diff --git a/jet.c b/jet.c
index 712269e..3deabde 100644
--- a/jet.c
+++ b/jet.c
@@ -11,6 +11,20 @@
#define NORMAL_MODE 0
#define INSERT_MODE 1
+struct buffer {
+ char *name;
+ struct point start;
+ struct point cursor;
+};
+
+struct buffer *new_buffer(char *name) {
+ struct buffer *result = calloc(1, sizeof(struct buffer));
+ result->name = name;
+ result->start.page = new_page();
+ result->cursor = result->start;
+ return result;
+}
+
int main(int argc, char *argv[]) {
initscr();
cbreak();
@@ -18,17 +32,16 @@ int main(int argc, char *argv[]) {
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
- struct point buffer_start = {new_page()};
- struct point window_start = buffer_start;
- struct point cursor = window_start;
+ struct buffer *buffer = new_buffer("test");
+ struct point window_start = buffer->start;
if (argc > 1) {
FILE *f = fopen(argv[1], "r");
char c;
while ((c = fgetc(f)) != EOF) {
- insert_at_point(&cursor, c);
+ insert_at_point(&buffer->cursor, c);
}
- cursor = window_start;
+ buffer->cursor = buffer->start;
fclose(f);
}
@@ -44,7 +57,7 @@ int main(int argc, char *argv[]) {
int x = -1, y = -1;
struct point window_end = window_start;
while (element(&window_end) && getcury(stdscr) < window_height - 1) {
- if (same_point(&window_end, &cursor)) {
+ if (same_point(&window_end, &buffer->cursor)) {
getyx(stdscr, y, x);
}
addch(element(&window_end));
@@ -65,22 +78,22 @@ int main(int argc, char *argv[]) {
mode = INSERT_MODE;
break;
case 'k':
- prev_line(&cursor, window_width);
+ prev_line(&buffer->cursor, window_width);
if (y <= 0) {
prev_line(&window_start, window_width);
}
break;
case 'j':
- next_line(&cursor, window_width);
+ next_line(&buffer->cursor, window_width);
if (y >= window_height - 2) {
next_line(&window_start, window_width);
}
break;
case 'h':
- move_point_backward(&cursor);
+ move_point_backward(&buffer->cursor);
break;
case 'l':
- move_point_forward(&cursor);
+ move_point_forward(&buffer->cursor);
break;
}
} else {
@@ -89,14 +102,14 @@ int main(int argc, char *argv[]) {
mode = NORMAL_MODE;
break;
case KEY_BACKSPACE:
- delete_at_point(&cursor);
+ delete_at_point(&buffer->cursor);
break;
default:
- insert_at_point(&cursor, input);
+ insert_at_point(&buffer->cursor, input);
}
}
- if (element(&cursor) == 0) {
- move_point_backward(&cursor);
+ if (element(&buffer->cursor) == 0) {
+ move_point_backward(&buffer->cursor);
}
}