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)
46 #define vi_flush() vi->tty.flush(vi->tty_cls)
48 static int remove_buf(struct visor *vi, struct vi_buffer *vb);
49 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size);
52 static const struct vi_alloc stdalloc = { malloc, free, realloc };
55 struct visor *vi_create(struct vi_alloc *mm)
60 if(!mm) mm = &stdalloc;
65 if(!(vi = mm->malloc(sizeof *vi))) {
68 memset(vi, 0, sizeof *vi);
77 void vi_destroy(struct visor *vi)
80 vi_delete_buf(vi, vi->buflist);
85 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop)
90 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty)
95 void vi_term_size(struct visor *vi, int xsz, int ysz)
98 vi->term_height = ysz;
101 void vi_redraw(struct visor *vi)
103 int i, col, cur_x = 0, cur_y = 0;
105 struct vi_buffer *vb;
106 struct vi_span *sp, *spans_end;
107 const char *tptr, *tend;
108 vi_addr spoffs, addr;
111 if(!(sp = vi_buf_find_span(vb, vb->view_start, &spoffs))) {
115 spans_end = vb->spans + vb->num_spans;
117 tptr = vi_buf_span_text(vb, sp);
118 tend = tptr + sp->size;
123 addr = vb->view_start;
124 for(i=0; i<vi->term_height; i++) {
126 col = -vb->view_xscroll - 1;
127 while(++col < vi->term_width && (c = (addr++, *tptr++)) != '\n') {
128 if(addr == vb->cursor) {
137 if(++sp >= spans_end) {
140 tptr = vi_buf_span_text(vb, sp);
141 tend = tptr + sp->size;
147 while(i < vi->term_height) {
148 vi_setcursor(0, i++);
152 vi_setcursor(cur_x, cur_y);
156 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
158 struct vi_buffer *nb;
160 if(!(nb = vi_malloc(sizeof *nb))) {
161 vi_error(vi, "failed to allocate new buffer\n");
164 memset(nb, 0, sizeof *nb);
168 if(vi_buf_read(nb, path) == -1) {
175 struct vi_buffer *last = vi->buflist->prev;
177 nb->next = vi->buflist;
179 vi->buflist->prev = nb;
181 nb->next = nb->prev = nb;
187 static int remove_buf(struct visor *vi, struct vi_buffer *vb)
190 vi_error(vi, "failed to remove a buffer which doesn't exist\n");
195 if(vi->buflist != vb) {
196 vi_error(vi, "failed to remove buffer, buffer list inconsistency\n");
203 if(vi->buflist == vb) {
204 vi->buflist = vb->next;
206 vb->prev->next = vb->next;
207 vb->next->prev = vb->prev;
208 vb->next = vb->prev = vb;
212 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb)
214 if(remove_buf(vi, vb) == -1) {
219 if(vb->file_mapped) {
233 int vi_num_buf(struct visor *vi)
236 struct vi_buffer *vb;
238 if(!vi->buflist) return 0;
241 vb = vi->buflist->next;
242 while(vb != vi->buflist) {
249 struct vi_buffer *vi_getcur_buf(struct visor *vi)
254 void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb)
259 struct vi_buffer *vi_next_buf(struct visor *vi)
261 return vi->buflist ? vi->buflist->next : 0;
264 struct vi_buffer *vi_prev_buf(struct visor *vi)
266 return vi->buflist ? vi->buflist->prev : 0;
269 /* split_span splits the span sp. if size > 0 it moves the second part to sp+2,
270 * leaving an empty place at sp+1 for the new span. The start point of the
271 * second part is adjusted by size.
273 * It can't fail, because it's always called with the span array having at
274 * least two empty slots (see: add_span).
276 void split_span(struct vi_buffer *vb, struct vi_span *sp, vi_addr spoffs, unsigned long size)
278 int newseg = size > 0 ? 1 : 0;
279 struct vi_span *tail = sp + newseg + 1;
280 int num_move = vb->spans + vb->num_spans - sp - 1;
282 memmove(tail + 1, sp + 1, num_move * sizeof *sp);
283 vb->num_spans += tail - sp;
287 tail->start += spoffs;
288 tail->size -= spoffs;
292 if(size <= tail->size) {
302 /* we produced one or more zero-sized spans, drop them */
303 num_move = vb->num_spans - (tail - sp);
304 memmove(sp, tail, num_move * sizeof *sp);
305 vb->num_spans -= num_move;
309 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size)
311 struct visor *vi = vb->vi;
315 /* make sure we have space for at least two new spans (split + add) */
316 if(vb->num_spans + 1 >= vb->max_spans) {
317 int newmax = vb->max_spans > 0 ? (vb->max_spans << 1) : 16;
318 struct vi_span *tmp = vi_realloc(vb->spans, newmax * sizeof *tmp);
321 vb->max_spans = newmax;
324 if((sp = vi_buf_find_span(vb, at, &spoffs))) {
326 split_span(vb, sp++, spoffs, 1);
328 split_span(vb, sp++, 0, 0);
331 sp = vb->spans + vb->num_spans;
341 void vi_buf_reset(struct vi_buffer *vb)
343 struct visor *vi = vb->vi;
344 struct vi_buffer *prev, *next;
349 if(vb->file_mapped) vi_unmap(vb->fp);
358 memset(vb, 0, sizeof *vb);
364 int vi_buf_read(struct vi_buffer *vb, const char *path)
366 struct visor *vi = vb->vi;
373 if(!(fp = vi_open(path, VI_RDONLY | VI_CREAT))) {
377 if(!(vb->path = vi_malloc(plen + 1))) {
378 vi_error(vi, "failed to allocate path name buffer\n");
382 memcpy(vb->path, path, plen + 1);
386 if((fsz = vi_size(fp))) {
387 /* existing file, map it into memory, or failing that read it */
388 if(!vi->fop.map || !(vb->orig = vi_map(fp))) {
389 if(!(vb->orig = vi_malloc(fsz))) {
397 if(add_span(vb, 0, SPAN_ORIG, 0, fsz) == -1) {
398 vi_error(vi, "failed to allocate span\n");
407 int vi_buf_write(struct vi_buffer *vb, const char *path)
410 struct visor *vi = vb->vi;
412 static char wbuf[512];
414 if(!path) path = vb->path;
416 vi_error(vi, "failed to write buffer, unknown path\n");
420 if(!(fp = vi_open(path, VI_WRONLY | VI_CREAT))) {
421 vi_error(vi, "failed to open %s for writing\n", path);
426 for(i=0; i<vb->num_spans; i++) {
427 struct vi_span *sp = vb->spans + i;
428 const char *sptxt = vi_buf_span_text(vb, sp);
430 while(count < sp->size) {
431 n = sp->size - count;
432 if(n > sizeof wbuf - wbuf_count) {
433 n = sizeof wbuf - wbuf_count;
435 memcpy(wbuf + wbuf_count, sptxt + count, n);
440 if(wbuf_count >= sizeof wbuf) {
441 vi_write(fp, wbuf, wbuf_count);
446 vi_write(fp, wbuf, wbuf_count);
452 long vi_buf_size(struct vi_buffer *vb)
457 for(i=0; i<vb->num_spans; i++) {
458 sz += vb->spans[i].size;
463 struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at, vi_addr *soffs)
466 long sz = 0, prev_sz;
468 for(i=0; i<vb->num_spans; i++) {
470 sz += vb->spans[i].size;
472 if(soffs) *soffs = at - prev_sz;
473 return vb->spans + i;
479 const char *vi_buf_span_text(struct vi_buffer *vb, struct vi_span *sp)
481 const char *buf = sp->src == SPAN_ORIG ? vb->orig : vb->add;
482 return buf + sp->start;