aboutsummaryrefslogtreecommitdiff
path: root/editor.h
blob: c10577d24bc7f498ffe54e733c004115b2a34283 (plain) (blame)
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
#include <stdbool.h>

typedef struct Editor Editor;
typedef struct Piece Piece;

typedef struct {
	const char const *start;  /* begin of piece's data */
	const char const *end;    /* pointer to the first byte after valid data i.e. [start, end) */
	const char const *text;   /* current position within piece: start <= text < end */
	const Piece const *piece; /* internal state do not touch! */
	size_t pos;               /* global position in bytes from start of file */
} Iterator;

#define editor_iterate(ed, it, pos) \
	for (Iterator it = editor_iterator_get((ed), (pos)); \
	     editor_iterator_valid(&it); \
	     editor_iterator_next(&it))

Editor *editor_load(const char *file);
const char *editor_filename(Editor*);
bool editor_insert(Editor*, size_t pos, const char *data);
bool editor_insert_raw(Editor*, size_t pos, const char *data, size_t len);
bool editor_delete(Editor*, size_t pos, size_t len);
bool editor_replace(Editor*, size_t pos, const char *data);
bool editor_replace_raw(Editor*, size_t pos, const char *data, size_t len);
void editor_snapshot(Editor*);
bool editor_undo(Editor*);
bool editor_redo(Editor*);

size_t editor_pos_by_lineno(Editor*, size_t lineno);
size_t editor_lineno_by_pos(Editor*, size_t pos);

size_t editor_bytes_get(Editor*, size_t pos, size_t len, char *buf);

Iterator editor_iterator_get(Editor*, size_t pos);
bool editor_iterator_valid(const Iterator*);
bool editor_iterator_next(Iterator*);
bool editor_iterator_prev(Iterator*);

bool editor_iterator_byte_get(Iterator *it, char *b);
bool editor_iterator_byte_next(Iterator*, char *b);
bool editor_iterator_byte_prev(Iterator*, char *b);

bool editor_iterator_char_next(Iterator *it, char *c);
bool editor_iterator_char_prev(Iterator *it, char *c);

typedef int Mark;
void editor_mark_set(Editor*, Mark, size_t pos);
size_t editor_mark_get(Editor*, Mark);
void editor_mark_clear(Editor*, Mark);
void editor_mark_clear_all(Editor*);

size_t editor_size(Editor*);
bool editor_modified(Editor*);
int editor_save(Editor*, const char *file);
void editor_free(Editor *ed);

// TMP
void editor_debug(Editor *ed);