2 visor - lightweight system-independent text editor and framework
3 Copyright (C) 2019 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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/>.
18 #ifndef LIB_VISOR_TEXTED_CORE_H_
19 #define LIB_VISOR_TEXTED_CORE_H_
22 typedef long vi_motion;
38 VI_MOT_WORD_NEXT = 'w',
39 VI_MOT_WORD_END = 'e',
40 VI_MOT_WORD_BEG = 'b',
41 VI_MOT_WORDP_NEXT = 'W',
42 VI_MOT_WORDP_BEG = 'B',
43 VI_MOT_LINE_BEG = '^',
44 VI_MOT_LINE_END = '$',
45 VI_MOT_SENT_NEXT = ')',
46 VI_MOT_SENT_PREV = '(',
47 VI_MOT_PAR_NEXT = '}',
48 VI_MOT_PAR_PREV = '{',
49 VI_MOT_SECT_NEXT = ']',
50 VI_MOT_SECT_PREV = '[',
51 VI_MOT_FIND_NEXT = 'f',
52 VI_MOT_FIND_PREV = 'F',
53 VI_MOT_FINDTO_NEXT = 't',
54 VI_MOT_FINDTO_PREV = 'T',
63 #define VI_MOTION(d, n) (((long)(n) << 8) | ((long)(d)))
66 void *(*malloc)(unsigned long);
68 void *(*realloc)(void*, unsigned long); /* can be null, will use malloc/free */
72 vi_file *(*open)(const char *path);
73 long (*size)(vi_file *file);
74 void (*close)(vi_file *file);
75 void *(*map)(vi_file *file);
76 void (*unmap)(vi_file *file);
77 long (*read)(vi_file *file, void *buf, long count);
78 long (*write)(vi_file *file, void *buf, long count);
79 long (*seek)(vi_file *file, long offs, int whence);
83 void (*clear)(void *cls);
84 void (*clear_line)(void *cls);
85 void (*clear_line_at)(int y, void *cls);
86 void (*setcursor)(int x, int y, void *cls);
87 void (*putchar)(char c, void *cls);
88 void (*putchar_at)(int x, int y, char c, void *cls);
89 void (*scroll)(int nlines, void *cls);
90 void (*del_back)(void *cls);
91 void (*del_fwd)(void *cls);
92 void (*status)(char *s, void *cls);
95 /* Create a new instance of the visor editor.
96 * The alloc argument can be used to provide custom memory allocation
97 * functions. It can be null in a hosted build, or if you set the HAVE_LIBC
98 * preprocessor macro, in which case the standard library allocator will be
101 struct visor *vi_create(struct vi_alloc *mm);
102 void vi_destroy(struct visor *vi);
104 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop);
105 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty);
107 /* vi_new_buf creates a new buffer and inserts it in the buffer list. If the
108 * path pointer is null, the new buffer will be empty, otherwise it's as if it
109 * was followed by a vi_buf_read call to read a file into the buffer.
111 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path);
112 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb);
113 int vi_num_buf(struct visor *vi);
115 struct vi_buffer *vi_getcur_buf(struct visor *vi);
116 void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb);
118 struct vi_buffer *vi_next_buf(struct visor *vi);
119 struct vi_buffer *vi_prev_buf(struct visor *vi);
122 /* reset a buffer to the newly created state, freeing all resources */
123 void vi_buf_reset(struct vi_buffer *vb);
125 /* Read a file into the buffer.
126 * Returns 0 on success, -1 on failure.
128 int vi_buf_read(struct vi_buffer *vb, const char *path);
130 /* Write the buffer out to a file. If the path is null, the buffer will be
131 * written out to the same file that was last read. If the path is null and
132 * no file was ever read in this buffer, the write fails.
133 * Returns 0 on success, -1 on failure.
135 int vi_buf_write(struct vi_buffer *vb, const char *path);
136 long vi_buf_size(struct vi_buffer *vb);
138 void vi_buf_ins_begin(struct vi_buffer *vb, vi_motion mot);
139 void vi_buf_insert(struct vi_buffer *vb, char *s);
140 void vi_buf_ins_end(struct vi_buffer *vb);
142 void vi_buf_del(struct vi_buffer *vb, vi_motion mot);
143 void vi_buf_yank(struct vi_buffer *vb, vi_motion mot);
145 #endif /* LIB_VISOR_TEXTED_CORE_H_ */