2 visor - lightweight system-independent embeddable text editor 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/>.
23 #define vi_malloc vi->mm.malloc
24 #define vi_free vi->mm.free
25 #define vi_realloc vi->mm.realloc
27 #define vi_open vi->fop.open
28 #define vi_size vi->fop.size
29 #define vi_close vi->fop.close
30 #define vi_map vi->fop.map
31 #define vi_unmap vi->fop.unmap
32 #define vi_read vi->fop.read
33 #define vi_write vi->fop.write
34 #define vi_seek vi->fop.seek
36 #define vi_clear() vi->tty.clear(vi->tty_cls)
37 #define vi_clear_line() vi->tty.clear_line(vi->tty_cls)
38 #define vi_clear_line_at(y) vi->tty.clear_line_at(y, vi->tty_cls)
39 #define vi_setcursor(x, y) vi->tty.setcursor(x, y, vi->tty_cls)
40 #define vi_putchar(c) vi->tty.putchar(c, vi->tty_cls)
41 #define vi_putchar_at(x, y, c) v->tty.putchar_at(x, y, c, vi->tty_cls)
42 #define vi_scroll(n) vi->tty.scroll(n, vi->tty_cls)
43 #define vi_del_back() vi->tty.del_back(vi->tty_cls)
44 #define vi_del_fwd() vi->tty.del_fwd(vi->tty_cls)
45 #define vi_status(s) vi->tty.status(s, vi->tty_cls)
47 static int remove_buf(struct visor *vi, struct vi_buffer *vb);
48 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size);
51 static const struct vi_alloc stdalloc = { malloc, free, realloc };
54 struct visor *vi_create(struct vi_alloc *mm)
59 if(!mm) mm = &stdalloc;
64 if(!(vi = mm->malloc(sizeof *vi))) {
67 memset(vi, 0, sizeof *vi);
76 void vi_destroy(struct visor *vi)
79 vi_delete_buf(vi, vi->buflist);
84 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop)
89 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty)
94 void vi_term_size(struct visor *vi, int xsz, int ysz)
97 vi->term_height = ysz;
100 void vi_redraw(struct visor *vi)
102 struct vi_buffer *vb;
103 struct vi_span *sp, *spend;
107 if(!(sp = vi_buf_find_span(vb, vi->view_start, &spoffs))) {
111 spend = vb->spans + vb->num_spans;
118 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
120 struct vi_buffer *nb;
122 if(!(nb = vi_malloc(sizeof *nb))) {
123 vi_error(vi, "failed to allocate new buffer\n");
126 memset(nb, 0, sizeof *nb);
130 if(vi_buf_read(nb, path) == -1) {
137 struct vi_buffer *last = vi->buflist->prev;
139 nb->next = vi->buflist;
141 vi->buflist->prev = nb;
143 nb->next = nb->prev = nb;
149 static int remove_buf(struct visor *vi, struct vi_buffer *vb)
152 vi_error(vi, "failed to remove a buffer which doesn't exist\n");
157 if(vi->buflist != vb) {
158 vi_error(vi, "failed to remove buffer, buffer list inconsistency\n");
165 if(vi->buflist == vb) {
166 vi->buflist = vb->next;
168 vb->prev->next = vb->next;
169 vb->next->prev = vb->prev;
170 vb->next = vb->prev = vb;
174 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb)
176 if(remove_buf(vi, vb) == -1) {
187 int vi_num_buf(struct visor *vi)
190 struct vi_buffer *vb;
192 if(!vi->buflist) return 0;
195 vb = vi->buflist->next;
196 while(vb != vi->buflist) {
203 struct vi_buffer *vi_getcur_buf(struct visor *vi)
208 void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb)
213 struct vi_buffer *vi_next_buf(struct visor *vi)
215 return vi->buflist ? vi->buflist->next : 0;
218 struct vi_buffer *vi_prev_buf(struct visor *vi)
220 return vi->buflist ? vi->buflist->prev : 0;
223 /* split_span splits the span sp. if size > 0 it moves the second part to sp+2,
224 * leaving an empty place at sp+1 for the new span. The start point of the
225 * second part is adjusted by size.
227 * It can't fail, because it's always called with the span array having at
228 * least two empty slots (see: add_span).
230 void split_span(struct vi_buffer *vb, struct vi_span *sp, vi_addr spoffs, unsigned long size)
232 int newseg = size > 0 ? 1 : 0;
233 struct vi_span *tail = sp + newseg + 1;
234 int num_move = vb->spans + vb->num_spans - sp - 1;
236 memmove(tail + 1, sp + 1, num_move * sizeof *sp);
237 vb->num_spans += tail - sp;
241 tail->start += spoffs;
242 tail->size -= spoffs;
246 if(size <= tail->size) {
256 /* we produced one or more zero-sized spans, drop them */
257 num_move = vb->num_spans - (tail - sp);
258 memmove(sp, tail, num_move * sizeof *sp);
259 vb->num_spans -= num_move;
263 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size)
265 struct visor *vi = vb->vi;
269 /* make sure we have space for at least two new spans (split + add) */
270 if(vb->num_spans + 1 >= vb->max_spans) {
271 int newmax = vb->max_spans > 0 ? (vb->max_spans << 1) : 16;
272 struct vi_span *tmp = vi_realloc(vb->spans, newmax * sizeof *tmp);
275 vb->max_spans = newmax;
278 if((sp = vi_buf_find_span(vb, at, &spoffs))) {
280 split_span(vb, sp++, spoffs, 1);
282 split_span(vb, sp++, 0, 0);
285 sp = vb->spans + vb->num_spans;
295 void vi_buf_reset(struct vi_buffer *vb)
297 struct visor *vi = vb->vi;
298 struct vi_buffer *prev, *next;
303 if(vb->file_mapped) vi_unmap(vb->fp);
312 memset(vb, 0, sizeof *vb);
318 int vi_buf_read(struct vi_buffer *vb, const char *path)
320 struct visor *vi = vb->vi;
327 if(!(fp = vi_open(path, VI_RDONLY | VI_CREAT))) {
331 if(!(vb->path = vi_malloc(plen + 1))) {
332 vi_error(vi, "failed to allocate path name buffer\n");
336 memcpy(vb->path, path, plen + 1);
340 if((fsz = vi_size(fp))) {
341 /* existing file, map it into memory, or failing that read it */
342 if(!vi->fop.map || !(vb->orig = vi_map(fp))) {
343 if(!(vb->orig = vi_malloc(fsz))) {
351 if(add_span(vb, 0, SPAN_ORIG, 0, fsz) == -1) {
352 vi_error(vi, "failed to allocate span\n");
361 int vi_buf_write(struct vi_buffer *vb, const char *path)
364 struct visor *vi = vb->vi;
366 static char wbuf[512];
368 if(!path) path = vb->path;
370 vi_error(vi, "failed to write buffer, unknown path\n");
374 if(!(fp = vi_open(path, VI_WRONLY | VI_CREAT))) {
375 vi_error(vi, "failed to open %s for writing\n", path);
380 for(i=0; i<vb->num_spans; i++) {
381 struct vi_span *sp = vb->spans + i;
382 const char *sptxt = vi_buf_span_text(vb, sp);
384 while(count < sp->size) {
385 n = sp->size - count;
386 if(n > sizeof wbuf - wbuf_count) {
387 n = sizeof wbuf - wbuf_count;
389 memcpy(wbuf + wbuf_count, sptxt + count, n);
394 if(wbuf_count >= sizeof wbuf) {
395 vi_write(fp, wbuf, wbuf_count);
400 vi_write(fp, wbuf, wbuf_count);
406 long vi_buf_size(struct vi_buffer *vb)
411 for(i=0; i<vb->num_spans; i++) {
412 sz += vb->spans[i].size;
417 struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at, vi_addr *soffs)
420 long sz = 0, prev_sz;
422 for(i=0; i<vb->num_spans; i++) {
424 sz += vb->spans[i].size;
426 if(soffs) *soffs = at - prev_sz;
427 return vb->spans + i;
433 const char *vi_buf_span_text(struct vi_buffer *vb, struct vi_span *sp)
435 const char *buf = sp->src == SPAN_ORIG ? vb->orig : vb->add;
436 return buf + sp->start;