1e79aed5ea82c111f6829b9532ba4cce88db3fad
[visor] / libvisor / include / visor.h
1 /*
2 visor - lightweight system-independent text editor and framework
3 Copyright (C)  2019 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #ifndef LIB_VISOR_TEXTED_CORE_H_
19 #define LIB_VISOR_TEXTED_CORE_H_
20
21 typedef long vi_addr;
22 typedef long vi_motion;
23 typedef void vi_file;
24
25 struct visor;
26 struct vi_buffer;
27
28 struct vi_span {
29         vi_addr start;
30         unsigned long size;
31         int src;
32 };
33
34 enum vi_motdir {
35         VI_MOT_LEFT                     = 'h',
36         VI_MOT_DOWN                     = 'j',
37         VI_MOT_UP                       = 'k',
38         VI_MOT_RIGHT            = 'l',
39         VI_MOT_WORD_NEXT        = 'w',
40         VI_MOT_WORD_END         = 'e',
41         VI_MOT_WORD_BEG         = 'b',
42         VI_MOT_WORDP_NEXT       = 'W',
43         VI_MOT_WORDP_BEG        = 'B',
44         VI_MOT_LINE_BEG         = '^',
45         VI_MOT_LINE_END         = '$',
46         VI_MOT_SENT_NEXT        = ')',
47         VI_MOT_SENT_PREV        = '(',
48         VI_MOT_PAR_NEXT         = '}',
49         VI_MOT_PAR_PREV         = '{',
50         VI_MOT_SECT_NEXT        = ']',
51         VI_MOT_SECT_PREV        = '[',
52         VI_MOT_FIND_NEXT        = 'f',
53         VI_MOT_FIND_PREV        = 'F',
54         VI_MOT_FINDTO_NEXT      = 't',
55         VI_MOT_FINDTO_PREV      = 'T',
56         VI_MOT_GO                       = 'G',
57         VI_MOT_TOP                      = 'H',
58         VI_MOT_MID                      = 'M',
59         VI_MOT_BOT                      = 'B',
60         VI_MOT_INNER            = 'i',
61         VI_MOT_OUTER            = 'a'
62 };
63
64 #define VI_MOTION(d, n) (((long)(n) << 8) | ((long)(d)))
65
66 struct vi_alloc {
67         void *(*malloc)(unsigned long);
68         void (*free)(void*);
69         void *(*realloc)(void*, unsigned long); /* can be null, will use malloc/free */
70 };
71
72 /* open flags (same as POSIX O_*) */
73 enum { VI_RDONLY, VI_WRONLY, VI_RDWR, VI_CREAT = 0x100 };
74 /* seek origin (same as C SEEK_*) */
75 enum { VI_SEEK_SET, VI_SEEK_CUR, VI_SEEK_END };
76
77
78 struct vi_fileops {
79         vi_file *(*open)(const char *path, unsigned int flags);
80         long (*size)(vi_file *file);
81         void (*close)(vi_file *file);
82         void *(*map)(vi_file *file);
83         void (*unmap)(vi_file *file);
84         long (*read)(vi_file *file, void *buf, long count);
85         long (*write)(vi_file *file, void *buf, long count);
86         long (*seek)(vi_file *file, long offs, int whence);
87 };
88
89 struct vi_ttyops {
90         void (*clear)(void *cls);
91         void (*clear_line)(void *cls);
92         void (*clear_line_at)(int y, void *cls);
93         void (*setcursor)(int x, int y, void *cls);
94         void (*putchar)(char c, void *cls);
95         void (*putchar_at)(int x, int y, char c, void *cls);
96         void (*scroll)(int nlines, void *cls);
97         void (*del_back)(void *cls);
98         void (*del_fwd)(void *cls);
99         void (*status)(char *s, void *cls);
100 };
101
102 /* Create a new instance of the visor editor.
103  * The alloc argument can be used to provide custom memory allocation
104  * functions. It can be null in a hosted build, or if you set the HAVE_LIBC
105  * preprocessor macro, in which case the standard library allocator will be
106  * used.
107  */
108 struct visor *vi_create(struct vi_alloc *mm);
109 void vi_destroy(struct visor *vi);
110
111 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop);
112 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty);
113
114 /* vi_new_buf creates a new buffer and inserts it in the buffer list. If the
115  * path pointer is null, the new buffer will be empty, otherwise it's as if it
116  * was followed by a vi_buf_read call to read a file into the buffer.
117  */
118 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path);
119 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb);
120 int vi_num_buf(struct visor *vi);
121
122 struct vi_buffer *vi_getcur_buf(struct visor *vi);
123 void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb);
124
125 struct vi_buffer *vi_next_buf(struct visor *vi);
126 struct vi_buffer *vi_prev_buf(struct visor *vi);
127
128
129 /* reset a buffer to the newly created state, freeing all resources */
130 void vi_buf_reset(struct vi_buffer *vb);
131
132 /* Read a file into the buffer.
133  * Returns 0 on success, -1 on failure.
134  */
135 int vi_buf_read(struct vi_buffer *vb, const char *path);
136
137 /* Write the buffer out to a file. If the path is null, the buffer will be
138  * written out to the same file that was last read. If the path is null and
139  * no file was ever read in this buffer, the write fails.
140  * Returns 0 on success, -1 on failure.
141  */
142 int vi_buf_write(struct vi_buffer *vb, const char *path);
143 long vi_buf_size(struct vi_buffer *vb);
144
145 /* find the span which corresponds to the specified text position
146  * if soffs is not null, the relative offset of the specified address from the
147  * start of the span is stored there.
148  */
149 struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at, vi_addr *soffs);
150 const char *vi_buf_span_text(struct vi_buffer *vb, struct vi_span *span);
151
152 void vi_buf_ins_begin(struct vi_buffer *vb, vi_motion mot);
153 void vi_buf_insert(struct vi_buffer *vb, char *s);
154 void vi_buf_ins_end(struct vi_buffer *vb);
155
156 void vi_buf_del(struct vi_buffer *vb, vi_motion mot);
157 void vi_buf_yank(struct vi_buffer *vb, vi_motion mot);
158
159 #endif  /* LIB_VISOR_TEXTED_CORE_H_ */