vis: improve switching to prompt mode
[vis.git] / text.h
blob685079e1aba986ec197dd34cef65d8cf206c69bb
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);
87 /* move to the next/previous UTF-8 encoded Unicode codepoint
88 * and set c (if it is non NULL) to the first byte */
89 bool text_iterator_codepoint_next(Iterator *it, char *c);
90 bool text_iterator_codepoint_prev(Iterator *it, char *c);
91 /* move to next/previous grapheme i.e. might skip over multiple
92 * Unicode codepoints (e.g. for combining characters) */
93 bool text_iterator_char_next(Iterator*, char *c);
94 bool text_iterator_char_prev(Iterator*, char *c);
96 typedef const char* Mark;
97 /* mark position `pos', the returned mark can be used to later retrieve
98 * the same text segment */
99 Mark text_mark_set(Text*, size_t pos);
100 /* get position of mark in bytes from start of the file or EPOS if
101 * the mark is not/no longer valid e.g. if the corresponding text was
102 * deleted. If the change is later restored the mark will once again be
103 * valid. */
104 size_t text_mark_get(Text*, Mark);
106 /* get position of change denoted by index, where 0 indicates the most recent */
107 size_t text_history_get(Text*, size_t index);
108 /* return the size in bytes of the whole text */
109 size_t text_size(Text*);
110 /* query whether the text contains any unsaved modifications */
111 bool text_modified(Text*);
112 /* query whether `addr` is part of a memory mapped region associated with
113 * this text instance */
114 bool text_sigbus(Text*, const char *addr);
116 /* which type of new lines does the text use? */
117 enum TextNewLine {
118 TEXT_NEWLINE_NL = 1,
119 TEXT_NEWLINE_CRNL,
122 enum TextNewLine text_newline_type(Text*);
124 /* save the whole text to the given `filename'. Return true if succesful.
125 * In which case an implicit snapshot is taken. The save might associate a
126 * new inode to file. */
127 bool text_save(Text*, const char *filename);
128 bool text_save_range(Text*, Filerange*, const char *file);
129 /* write the text content to the given file descriptor `fd'. Return the
130 * number of bytes written or -1 in case there was an error. */
131 ssize_t text_write(Text*, int fd);
132 ssize_t text_write_range(Text*, Filerange*, int fd);
133 /* release all ressources associated with this text instance */
134 void text_free(Text*);
136 #endif