forked from tsoding/ded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.h
55 lines (45 loc) · 1.65 KB
/
editor.h
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
#ifndef EDITOR_H_
#define EDITOR_H_
#include <stdlib.h>
typedef struct {
size_t begin;
size_t end;
} Line;
typedef struct {
char *items;
size_t count;
size_t capacity;
} Data;
typedef struct {
Line *items;
size_t count;
size_t capacity;
} Lines;
#define DA_INIT_CAP 256
#define da_append(da, item) \
do { \
if ((da)->count >= (da)->capacity) { \
(da)->capacity = (da)->capacity == 0 ? DA_INIT_CAP : (da)->capacity*2; \
(da)->items = realloc((da)->items, (da)->capacity*sizeof(*(da)->items)); \
assert((da)->items != NULL && "Buy more RAM lol"); \
} \
\
(da)->items[(da)->count++] = (item); \
} while (0)
typedef struct {
Data data;
Lines lines;
size_t cursor;
} Editor;
void editor_save_to_file(const Editor *editor, const char *file_path);
void editor_load_from_file(Editor *editor, FILE *file);
void editor_backspace(Editor *editor);
void editor_delete(Editor *editor);
size_t editor_cursor_row(const Editor *e);
void editor_move_line_up(Editor *e);
void editor_move_line_down(Editor *e);
void editor_move_char_left(Editor *e);
void editor_move_char_right(Editor *e);
void editor_insert_char(Editor *e, char x);
void editor_recompute_lines(Editor *e);
#endif // EDITOR_H_