travis: try to fix build by using local built dependencies
[vis.git] / text.h
blobd338e0b0d267e297053ff4b67bbf4fc52e8645df
1 #ifndef TEXT_H
2 #define TEXT_H
4 #include <stdbool.h>
5 #include <time.h>
6 #include <unistd.h>
7 #include <stdarg.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
11 #define EPOS ((size_t)-1) /* invalid position */
13 typedef size_t Filepos;
15 typedef struct {
16 size_t start, end; /* range in bytes from start of the file */
17 } Filerange;
19 typedef struct Text Text;
20 typedef struct Piece Piece;
22 typedef struct {
23 const char *start; /* begin of piece's data */
24 const char *end; /* pointer to the first byte after valid data i.e. [start, end) */
25 const char *text; /* current position within piece: start <= text < end */
26 const Piece *piece; /* internal state do not touch! */
27 size_t pos; /* global position in bytes from start of file */
28 } Iterator;
30 #define text_iterate(txt, it, pos) \
31 for (Iterator it = text_iterator_get((txt), (pos)); \
32 text_iterator_valid(&it); \
33 text_iterator_next(&it))
35 /* create a text instance populated with the given file content, if `filename'
36 * is NULL the text starts out empty */
37 Text *text_load(const char *filename);
38 /* file information at time of load or last save */
39 struct stat text_stat(Text*);
40 bool text_appendf(Text*, const char *format, ...);
41 bool text_printf(Text*, size_t pos, const char *format, ...);
42 bool text_vprintf(Text*, size_t pos, const char *format, va_list ap);
43 /* insert `len' bytes starting from `data' at `pos' which has to be
44 * in the interval [0, text_size(txt)] */
45 bool text_insert(Text*, size_t pos, const char *data, size_t len);
46 /* delete `len' bytes starting from `pos' */
47 bool text_delete(Text*, size_t pos, size_t len);
48 bool text_delete_range(Text*, Filerange*);
49 /* mark the current text state, such that it can be {un,re}done */
50 void text_snapshot(Text*);
51 /* undo/redo to the last snapshotted state. returns the position where
52 * the change occured or EPOS if nothing could be {un,re}done. */
53 size_t text_undo(Text*);
54 size_t text_redo(Text*);
55 /* move chronlogically to the `count' earlier/later revision */
56 size_t text_earlier(Text*, int count);
57 size_t text_later(Text*, int count);
58 /* restore the text to the state closest to the time given */
59 size_t text_restore(Text*, time_t);
60 /* get creation time of current state */
61 time_t text_state(Text*);
63 size_t text_pos_by_lineno(Text*, size_t lineno);
64 size_t text_lineno_by_pos(Text*, size_t pos);
66 /* set `buf' to the byte found at `pos' and return true, if `pos' is invalid
67 * false is returned and `buf' is left unmodified */
68 bool text_byte_get(Text*, size_t pos, char *buf);
69 /* store at most `len' bytes starting from `pos' into `buf', the return value
70 * indicates how many bytes were copied into `buf'. WARNING buf will not be
71 * NUL terminated. */
72 size_t text_bytes_get(Text*, size_t pos, size_t len, char *buf);
74 Iterator text_iterator_get(Text*, size_t pos);
75 bool text_iterator_valid(const Iterator*);
76 bool text_iterator_next(Iterator*);
77 bool text_iterator_prev(Iterator*);
79 /* get byte at current iterator position, if this is at EOF a NUL
80 * byte (which is not actually part of the file) is read. */
81 bool text_iterator_byte_get(Iterator*, char *b);
82 /* advance iterator by one byte and get byte at new position. */
83 bool text_iterator_byte_prev(Iterator*, char *b);
84 /* if the new position is at EOF a NUL byte (which is not actually
85 * part of the file) is read. */
86 bool text_iterator_byte_next(Iterator*, char *b);
88 bool text_iterator_char_next(Iterator*, char *c);
89 bool text_iterator_char_prev(Iterator*, char *c);
91 typedef const char* Mark;
92 /* mark position `pos', the returned mark can be used to later retrieve
93 * the same text segment */
94 Mark text_mark_set(Text*, size_t pos);
95 /* get position of mark in bytes from start of the file or EPOS if
96 * the mark is not/no longer valid e.g. if the corresponding text was
97 * deleted. If the change is later restored the mark will once again be
98 * valid. */
99 size_t text_mark_get(Text*, Mark);
101 /* get position of change denoted by index, where 0 indicates the most recent */
102 size_t text_history_get(Text*, size_t index);
103 /* return the size in bytes of the whole text */
104 size_t text_size(Text*);
105 /* query whether the text contains any unsaved modifications */
106 bool text_modified(Text*);
107 /* query whether `addr` is part of a memory mapped region associated with
108 * this text instance */
109 bool text_sigbus(Text*, const char *addr);
111 /* which type of new lines does the text use? */
112 enum TextNewLine {
113 TEXT_NEWLINE_NL = 1,
114 TEXT_NEWLINE_CRNL,
117 enum TextNewLine text_newline_type(Text*);
119 /* save the whole text to the given `filename'. Return true if succesful.
120 * In which case an implicit snapshot is taken. The save might associate a
121 * new inode to file. */
122 bool text_save(Text*, const char *filename);
123 bool text_save_range(Text*, Filerange*, const char *file);
124 /* write the text content to the given file descriptor `fd'. Return the
125 * number of bytes written or -1 in case there was an error. */
126 ssize_t text_write(Text*, int fd);
127 ssize_t text_write_range(Text*, Filerange*, int fd);
128 /* release all ressources associated with this text instance */
129 void text_free(Text*);
131 #endif